file_get_contents无法打开流:没有这样的文件或目录

时间:2015-02-11 16:50:12

标签: php

我得到了一个相当奇怪的错误

file_get_contents('/var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt'): failed to open stream: No such file or directory

所以我将代码更改为:

if (file_exists($cookie_file)) {
    echo file_get_contents($cookie_file);
} else {
    echo "$cookie_file does not exist\n";
}

它输出:

/var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt does not exist

如果我这样做在命令行中

cat /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt

它使文件很好

权限也很好:

ls -lrt /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt
-rwxrwxrwx. 1 apache apache 391 Feb 11 16:27 /var/www/site/config/../runtime/cookies/1a8dd816aafc1118dc5cbf7fd0263f20/cookie.txt

所以是的,我对它为何不能阅读明显存在的文件感到困惑。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用realpath扩展绝对路径的相对路径。路径中..config之间的runtime表示其相对值,而file_get_contents需要绝对值。