在php类中找不到文件异常

时间:2013-08-19 15:32:19

标签: php class path fopen file-not-found

您好,感谢您的时间。 我在使用以下代码时遇到问题:

//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 var并且没关系(“../ chat / 1.2 / messages.txt”);

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

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

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

解决方案

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

1 个答案:

答案 0 :(得分:1)

您的功能定义为private

private function readFile($fileName)

之外的代码类无法直接调用其中的private函数。