即使文件存在于我的linux上,php file_exists也会返回false [英] php file_exists is returning false even if file exist on my linux

查看:3034
本文介绍了即使文件存在于我的linux上,php file_exists也会返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问了很多次了,但是我找到的答案都没有帮到我。

我试图让php file_exists()工作。当它工作的唯一情况是当php文件是在文件相同的目录中使用file_exist()并且只使用文件名(即不包括路径)。但这不是随之而来的行为,请看下面。

索取信息:


  • safe_mode = off

  • 目录28没有符号链接
  • 文件名没有空格

  • / var / www / html / smic / upload中的所有目录/ 28 /具有apache:apache 777作为权限。 使用php 5.3


PHP:

  echo getcwd()
clearstatcache();
$ file ='file:///var/www/html/smic/upload/28/ul.txt';
//也试过这样
// $ file ='/var/www/html/smic/upload/28/ul.txt';

if(file_exists($ file)){
echo $ file。exists;




$ b getcwd()打印/ var / www / html / smic / modules / core / ticket



php-script和要检查的文件的权限是apache:apache 777.

有关目录结构的一些细节:

$ p $ [root @ localhost 28]#pwd
/ var / www / html / smic / upload / 28

[root @ localhost 28]#ls -l ul.txt
-rw-r - r--。 1 apache apache 2 2月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

更改后行为没有改变文件的许可。目录/ 28有drwxr-xr-x。对于apache用户和apache组

为了测试,我也把实际的php脚本移到了/ 28,给了它apache:apache 777 rigths。将$文件改为ul.txt(即$ file ='ul.txt';)。这工作,找到ul.txt文件。


$ b $ getcwd()打印然后/ var / www / html / smic / upload / 28

我试图找到另一个文件,不包括ticket目录中的路径,文件不被识别。



我正在敲我的头...

任何建议表示赞赏。

解决方案

啧啧,这是一个很漂亮的凝灰岩。我总是确保当我从网上复制任何东西,首先粘贴到一个正常的texteditor为了删除所有奇怪的/隐藏的字符,然后复制什么,并粘贴到我的开发工具。

正如我在评论中提到的那样,我做了pwd并将我的虚拟服务器的文本复制到了我的osx中。但是,我没有想到/知道的是,如果我从我的Linux服务器上做到这一点,奇怪的/隐藏的字符可以遵循,因此我没有确保通过texteditor复制所有的东西。

我记得当前我在网上复制时遇到了问题,认为这可能是一个类似的问题。我用十六进制编辑器打开了我的脚本。我发现了什么...'/ var'看起来很简洁:'/ var'。去除奇怪的字符解决了这个问题。

谢谢大家以上的意见。希望那些可以帮助别人,也许它甚至不知道它帮助我(因为我根据你的意见做了很多事情)。

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.

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

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

The permission of the php-script and the file to be checked is 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

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.

getcwd() prints then /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.

I'm banging my head...

Any advice is appreciated.

解决方案

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.

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.

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).

这篇关于即使文件存在于我的linux上,php file_exists也会返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆