php类中未发现文件异常


File not found exception inside php class

您好,感谢您抽出时间。我对以下代码有问题:

//reads all the data from $fileName and returns it
private function readFile($fileName)
{
    $location = $this->path.$fileName;
    try{//try to open file
        $file=fopen($location,"r");
    }
    catch(Exception $e){
        return $e;
    }
    $return = "";//read file contents
    while (!feof($file))
    {
        $return .= fgetc($file);
    }
    //close file and return result
    fclose($file);
    return $return;
}

我在一个类中有这个函数,但每次调用fopen时,它都会抛出以下异常:

Warning: readfile(messages.txt): failed to open stream: No such file or directory in C:'xampp'htdocs'zdupp'php'textChat.php on line 85

但我检查了$location变量,它是可以的("../chat/1.2/messages.txt");

文件也在那里。我还尝试了一条从C:开始的路径

C:/xampp/htdocs/zdupp/chat/1.2/messages.txt

但没有成功。你能帮我一下吗?

解决方案

readFile()和$this->readFile(()是不同的函数。代码是正确的,但它从未被调用。谢谢你的回复。

您的函数定义为private:

private function readFile($fileName)

外部的代码,类将无法直接调用其内部的private函数。