问题是,我在与body.html位于同一文件夹的类中使用了file _ get _ contents("body.html")方法。问题是我收到一个错误,说找不到该文件。这是因为来自另一个类的我需要使用方法file _ get _ contents("body.html")的文件,而突然我不得不使用"../body/body.html“作为文件路径。
这不是有点奇怪吗?调用方法file _ get _ contents("body.html")的类与body.html在同一个文件夹中,但是因为这个类需要来自其他地方的另一个类,所以我需要一个新的文件路径?!
下面是目录和文件的简短列表:
lib/main/main.php
lib/body/body.php
lib/body/body.html
这是body.php:
class Body {
public function getOutput(){
return file_get_contents("body.html");
}
}这是main.php:
require '../body/body.php';
class Main {
private $title;
private $body;
function __construct() {
$this->body = new Body();
}
public function setTitle($title) {
$this->title = $title;
}
public function getOutput(){
//prints html with the body and other stuff..
}
}
$class = new Main();
$class->setTitle("Tittel");
echo $class->getOutput();我所要求的是修复错误,即body.php与body.html在同一文件夹中,但是当另一个类需要来自file _ get _ contents("body.html")方法中其他位置的body.php时,我必须更改路径
谢谢!
发布于 2009-11-11 05:25:39
不,这并不奇怪。
PHP与工作目录一起运行。这通常是"entry“脚本所在的目录。执行包含的脚本时,此工作目录不会更改。
如果您想要读取当前执行文件目录中的内容,请尝试类似这样的操作
$path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR."body.php";https://stackoverflow.com/questions/1711281
复制相似问题