【发布时间】:2014-03-30 00:38:35
【问题描述】:
当我在像/a/path/to/a/../file.php 这样的路径上使用file_get_contents 时,它得到的内容就好了。如果我先调用file_exists(或is_file 或realpath),则返回值表明该文件不存在。似乎是什么问题?
编辑:以下是从 cmets 浓缩到答案的一些附加信息:
- 我正在运行带有 php 5.5.6 的 Mac OS X 10.9,所以 安全模式应该不是问题 (it was removed in version 5.4)
- 我尝试通过调用
clearstatcache(true, $dir1)清除文件现金 - 有问题的文件大小为 362 字节,但我用几个不同的文件在多个位置复制了这个问题。
-
open_basedir在 php.ini 中被注释掉 - 文件是本地的(我尝试的第一个文件与脚本在同一目录中)
- 命令行 (phpUnit) 和浏览器中存在问题。
- 有问题的文件的权限是
-rwxrwxrwx(我 sudo-chmod-777ed 文件)
这是一个创建行为的代码 sn-p:
$dir1 = '/a/path/to/a/../file.php';
$dir2 = '/a/path/to/file.php';
echo "File content dir1:\n";
echo file_get_contents($dir1);
echo "\ndir1 exists: ".(int)file_exists($dir1);
echo "\n\nFile content dir2:\n";
echo file_get_contents($dir2);
echo "\ndir2 exists: ".(int)file_exists($dir2);
输出是:
File content dir1:
The actual content of the file. I promise!
dir1 exists: 0
File content dir2:
The actual content of the file. I promise!
dir2 exists: 1
【问题讨论】:
-
您可以在此处发布您要比较的具体代码吗?
file_exists()应该可以很好地处理这样的路径。 -
我无法重现这种行为——我尝试了相对路径和绝对路径,都包括'..'
-
@谁投票结束“不清楚你在问什么”,请说明你觉得不清楚的地方,我很乐意解决你的问题。
标签: php file-exists