即使我的 linux 上存在文件,php file_exists 也会返回 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9211928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php file_exists is returning false even if file exist on my linux
提问by Nicsoft
This question has been asked many times, but none of the answers I found helped me.
这个问题已被问过很多次,但我找到的答案都没有帮助我。
I am trying to get php file_exists() to work. The only scenario when it works is when the php-file is in the same directory as the file to use file_exist() on and only using the file name (i.e. excluding the path). But it's not consequent behaviour, please see below.
我试图让 php file_exists() 工作。它工作的唯一情况是当 php 文件与要使用 file_exist() 的文件位于同一目录中并且仅使用文件名(即不包括路径)时。但这不是随之而来的行为,请参见下文。
Som information:
索姆信息:
- safe_mode=off
- no symlinks for directory 28
- no whitespace in the file name
- All directories in /var/www/html/smic/upload/28/ has apache:apache 777 as permission.
- Using php 5.3
- 安全模式=关闭
- 目录 28 没有符号链接
- 文件名中没有空格
- /var/www/html/smic/upload/28/ 中的所有目录都具有 apache:apache 777 作为权限。
- 使用 PHP 5.3
PHP:
PHP:
echo getcwd()
clearstatcache();
$file = 'file:///var/www/html/smic/upload/28/ul.txt';
//Also tried like this
//$file = '/var/www/html/smic/upload/28/ul.txt';
if(file_exists($file)){
echo $file." exists";
}
getcwd() prints /var/www/html/smic/modules/core/ticket
getcwd() 打印 /var/www/html/smic/modules/core/ticket
The permission of the php-script and the file to be checked is apache:apache 777.
php-script和要检查的文件的权限是apache:apache 777。
Some details about the directory structure:
关于目录结构的一些细节:
[root@localhost 28]# pwd
/var/www/html/smic/upload/28
[root@localhost 28]# ls -l ul.txt
-rw-r--r--. 1 apache apache 2 Feb 9 10:50 ul.txt
[root@localhost 28]# chmod 777 ul.txt
[root@localhost 28]# ls -l ul.txt
-rwxrwxrwx. 1 apache apache 2 Feb 9 10:50 ul.txt
The behaviour didn't change after changing the permission of the file. The directory /28 has drwxr-xr-x. for apache user and apache group
更改文件的权限后,行为没有改变。目录 /28 有 drwxr-xr-x。对于 apache 用户和 apache 组
For test, I also moved the actual php-script to /28, gave it apache:apache 777 rigths. Changed the $file to "ul.txt" (i.e. $file = 'ul.txt';). That works, the ul.txt file is found.
为了测试,我还将实际的 php 脚本移到 /28,给它 apache:apache 777 rigths。将 $file 更改为“ul.txt”(即 $file = 'ul.txt';)。成功了,找到了 ul.txt 文件。
getcwd() prints then /var/www/html/smic/upload/28
getcwd() 然后打印 /var/www/html/smic/upload/28
As another test I tried to find another file excluding the path in the "ticket" directory, the file wasn't recognized.
作为另一项测试,我试图在“ticket”目录中找到不包括路径的另一个文件,但无法识别该文件。
I'm banging my head...
我在敲我的头...
Any advice is appreciated.
任何建议表示赞赏。
采纳答案by Nicsoft
Gee, this was a pretty tuff one. I always make sure when I copy anything from the web to first paste it into a normal texteditor in order to remove all strange/hidden characters, then copy the whatever again and paste it into my dev tool.
哎呀,这是一个漂亮的凝灰岩。我总是确保当我从网上复制任何东西时首先将其粘贴到普通的文本编辑器中以删除所有奇怪/隐藏的字符,然后再次复制任何内容并将其粘贴到我的开发工具中。
As I mentioned somewhere in a comment, I did pwd and copied the text form my virtual server to my osx. But what I didn't think about/know was that strange/hidden characters could follow if I did that from my linux server, hence I didn't make sure to copy everything via texteditor.
正如我在评论中提到的那样,我做了 pwd 并将文本从我的虚拟服务器复制到我的 osx。但是我没有想到/知道的是,如果我从我的 linux 服务器上这样做,可能会出现奇怪/隐藏的字符,因此我没有确保通过文本编辑器复制所有内容。
I remembered that I had problem quite some time ago when I copied from the web and figured this may be a similar kind of problem. I opened my script in an hex editor. And what did I find... '/var' looked lite this: '???/var'. Removing the strange characters fixed the problem.
我记得很久以前当我从网上复制并认为这可能是类似的问题时,我遇到了问题。我在十六进制编辑器中打开了我的脚本。我发现了什么......'/var'看起来很简单:'???/var'。删除奇怪的字符解决了这个问题。
Thank you all for your comments above. Hope those can help someone else and perhaps it has helped me without even knowing it (since I did a lot of things based on your comments).
感谢大家以上的评论。希望那些可以帮助别人,也许它在不知不觉中帮助了我(因为我根据你的评论做了很多事情)。
回答by Garry Welding
From the php.net manual, "Upon failure, an E_WARNING is emitted." make sure error reporting is set to a level that will show warnings and try it.
来自 php.net 手册,“失败时,会发出 E_WARNING。” 确保错误报告设置为显示警告并尝试的级别。
Also, "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.". You're not running in php safe mode are you? phpinfo() should tell you.
此外,“对于由于安全模式限制而无法访问的文件,此函数返回 FALSE。但是,如果这些文件位于 safe_mode_include_dir 中,则仍然可以包含这些文件。”。你不是在 php 安全模式下运行吗?phpinfo() 应该告诉你。
回答by Timothée Groleau
The permission of the file alone is not sufficient for php to assess file_exists. the process that runs php also needs permission to traverse all the parent directories of that file to get to it.
仅靠文件的权限不足以让 php 评估 file_exists。运行 php 的进程也需要遍历该文件的所有父目录才能访问它的权限。
Check them one by one and verify that php can read and enter (r-x)
一一检查,验证php可以读入(rx)
ls -ald /var
ls -ald /var/www
ls -ald /var/www/html
ls -ald /var/www/html/smic
ls -ald /var/www/html/smic/upload
ls -ald /var/www/html/smic/upload/28
回答by James
Where is your php file located? If within the "html" folder, then use a relative path like $file = "smic/upload/28/ul.txt";
你的 php 文件在哪里?如果在“html”文件夹中,则使用相对路径,如$file = "smic/upload/28/ul.txt";
回答by FreudianSlip
Clutching at straws here...
在这里抓着稻草……
Try su - apache (or whatever the user your webserver runs as.. apache2, www-data etc..) and then traversing the directory.
尝试 su - apache (或任何您的网络服务器以 .. apache2、www-data 等身份运行的用户),然后遍历该目录。
Are there any .htaccess files knocking about, or any other restrictions on your apache configuration?
是否有任何 .htaccess 文件在敲门,或者对您的 apache 配置有任何其他限制?
回答by yen1k
what's about the access rights to the folder?
the path $file = 'file:///var/www/html/smic/upload/28/ul.txt';
is incorrect.
Try the php script copy to folder upload and file='28/ul.txt';
.
The folder must be readable.
文件夹的访问权限是什么?路径$file = 'file:///var/www/html/smic/upload/28/ul.txt';
不正确。尝试将 php 脚本复制到文件夹上传和file='28/ul.txt';
. 该文件夹必须是可读的。
回答by Userbn
Try to open the file using $file = '/upload/28/ul.txt';
尝试使用 $file = '/upload/28/ul.txt' 打开文件;
回答by frops
Perhaps, in a name of the file there is a whitespace symbol. It's important.
也许,在文件名中有一个空格符号。这一点很重要。