php file_exists 路径,php – file_exists和包含相对路径的路径(“/../”)

当我在/a/path/to/a/../file.php之类的路径上使用file_get_contents时,它获取内容就好了.如果我先调用file_exists(或is_file或realpath),则返回值表示该文件不存在.这似乎是什么问题?

编辑:以下是从评论到答案浓缩的一些其他信息:

>我使用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文件)

这是一个创建行为的代码段:

$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

解决方法:

听起来你打开了安全模式并试图访问PHP在安全模式下运行时认为不安全的文件. From the manual:

Warning

This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

编辑:如果/ a / path / to / a /不是真实路径,您也可以重现此行为.例如:

$dir1 = '/realDir/realDir2/filetoinclude.php';

echo "File content dir1:\n";

echo file_get_contents($dir1); // outputs file contents

echo "\ndir1 exists: ".(int)file_exists($dir1); // outputs 1

$dir2 = '/realDir/realDir2/realDir3/../filetoinclude.php';

echo "\n\nFile content dir2:\n";

echo file_get_contents($dir2); // outputs file contents

echo "\ndir2 exists: ".(int)file_exists($dir2); // outputs 1

$dir3 = '/realDir/realDir2/NotARealDirectory/../filetoinclude.php';

echo "\n\nFile content dir3:\n";

echo file_get_contents($dir3); // outputs file contents

echo "\ndir3 exists: ".(int)file_exists($dir3); // outputs 0

这是因为file_exists需要遍历整个路径,因此它会查找丢失的目录并失败.我不确定file_get_contents到底有什么不同,我在谷歌上找不到多少,但它显然对路径的解析与file_exists的做法不同.

标签:php,file-exists

来源: https://codeday.me/bug/20190517/1120300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值