ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-05-17 09:26:13  阅读:350  来源: 互联网

标签: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 /不是真实路径,您也可以重现此行为.例如:

<?php

$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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有