即使文件存在于我的 Linux 上,php file_exists 也会返回 false

发布于 2025-01-03 15:25:17 字数 1333 浏览 3 评论 0原文

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

我正在尝试让 php file_exists() 工作。它起作用的唯一情况是 php 文件与要使用 file_exist() 的文件位于同一目录中并且仅使用文件名(即不包括路径)。但这不是随之而来的行为,请参见下文。

Som 信息:

  • 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';
//Also tried like this
//$file = '/var/www/html/smic/upload/28/ul.txt';

if(file_exists($file)){
    echo $file." exists";
}

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

php 脚本和要检查的文件的权限是 apache:apache 777。

目录结构的一些详细信息:

[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

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

为了测试,我还将实际的 php 脚本移动到 /28,给它 apache:apache 777 权限。将 $file 更改为“ul.txt”(即 $file = 'ul.txt';)。成功了,找到了 ul.txt 文件。

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

作为另一个测试,我试图找到另一个文件,不包括“ticket”目录中的路径,该文件未被识别。

我正在敲头...

任何建议都会受到赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

帅的被狗咬 2025-01-10 15:25:17

仅文件的权限不足以让 php 评估 file_exists。运行 php 的进程还需要遍历该文件的所有父目录才能访问该文件的权限。

一一检查,验证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

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.

Check them one by one and verify that php can read and enter (r-x)

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
梅倚清风 2025-01-10 15:25:17

哎呀,这真是一个漂亮的凝灰岩。我总是确保当我从网上复制任何内容时,首先将其粘贴到普通的文本编辑器中,以删除所有奇怪/隐藏的字符,然后再次复制所有内容并将其粘贴到我的开发工具中。

正如我在评论中提到的,我执行了 pwd 并将文本从我的虚拟服务器复制到我的 osx。但我没有想到/知道的是,如果我从 Linux 服务器上执行此操作,可能会出现奇怪/隐藏的字符,因此我没有确保通过文本编辑器复制所有内容。

我记得很久以前我从网上复制时遇到了问题,并认为这可能是类似的问题。我在十六进制编辑器中打开了脚本。我发现了什么...“/var”看起来很简单:“/var”。删除奇怪的字符解决了问题。

谢谢大家上面的评论。希望这些可以帮助别人,也许它甚至在不知情的情况下帮助了我(因为我根据你的评论做了很多事情)。

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

吻风 2025-01-10 15:25:17

在这里抓住救命稻草......

尝试 su - apache (或者你的网络服务器运行的任何用户......apache2,www-data 等),然后遍历目录。

是否有任何 .htaccess 文件存在,或者您的 apache 配置有任何其他限制?

Clutching at straws here...

Try su - apache (or whatever the user your webserver runs as.. apache2, www-data etc..) and then traversing the directory.

Are there any .htaccess files knocking about, or any other restrictions on your apache configuration?

拍不死你 2025-01-10 15:25:17

该文件夹的访问权限如何?
路径 $file = 'file:///var/www/html/smic/upload/28/ul.txt'; 不正确。
尝试将 php 脚本复制到文件夹 upload 和 file='28/ul.txt';
该文件夹必须可读。

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.

玻璃人 2025-01-10 15:25:17

尝试使用 $file = '/upload/28/ul.txt'; 打开文件

Try to open the file using $file = '/upload/28/ul.txt';

如果没有你 2025-01-10 15:25:17

根据 php.net 手册,“失败时,会发出 E_WARNING”。确保错误报告设置为将显示警告的级别并尝试。

另外,“对于由于安全模式限制而无法访问的文件,此函数返回 FALSE。但是,如果这些文件位于 safe_mode_include_dir 中,则仍然可以包含它们。”。你没有在 php 安全模式下运行,是吗? phpinfo() 应该告诉你。

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.

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.

不回头走下去 2025-01-10 15:25:17

你的 php 文件位于哪里?如果在“html”文件夹中,则使用相对路径,例如 $file = "smic/upload/28/ul.txt";

Where is your php file located? If within the "html" folder, then use a relative path like $file = "smic/upload/28/ul.txt";

薄凉少年不暖心 2025-01-10 15:25:17

也许,在文件名中存在空格符号。这很重要。

Perhaps, in a name of the file there is a whitespace symbol. It's important.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文