【问题标题】:File not found exception inside php class在php类中找不到文件异常
【发布时间】:2013-08-19 15:32:19
【问题描述】:

您好,感谢您的宝贵时间。 我对以下代码有疑问:

//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() 是不同的函数。代码没问题,但从未被调用。感谢您的回复。

【问题讨论】:

  • C:\xampp\htdocs\zdupp\ != C:/xampp/htdocs/zdupp/
  • 您是否尝试将$this->path.$filename 的值替换为/xampp/htdocs/zdupp/chat/1.2/messages.txt?该文件的权限是什么?运行PHP的用户是否有读写权限?这在 Windows 下仍然相关
  • 查看echo $location中的内容
  • @Anigel 正斜杠在 PHP windows 路径中用于反斜杠
  • 顺便说一句,@Anigel Windows 并不关心你的路径是 / 还是 `. I just tested it and it works fine either way. If you use ` 你需要转义它。

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


【解决方案1】:

你的函数定义为private:

private function readFile($fileName)

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

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2017-05-15
    • 1970-01-01
    相关资源
    最近更新 更多