php file_exists 返回 false 即使我的 linux 上存在文件

标签 php linux file-upload file-exists

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

我正在尝试让 php file_exists() 工作。它工作的唯一场景是当 php 文件与要使用 file_exist() 的文件位于同一目录中并且仅使用文件名(即不包括路径)时。但这不是结果行为,请参见下文。

索姆信息:

  • 安全模式=关闭
  • 目录 28 没有符号链接(symbolic link)
  • 文件名中没有空格
  • /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”目录中查找除路径之外的另一个文件,但无法识别该文件。

我在敲我的头...

欢迎任何建议。

最佳答案

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

一一检查,验证php可以读取和输入(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

关于php file_exists 返回 false 即使我的 linux 上存在文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211928/

相关文章:

javascript - joomla 3.2后端javascript错误

php - 如何从按日期存储在数组中的表中获取所有用户?

linux - 调整 xargs 以接受 ls -lh

linux - 在 Linux 上禁用键盘

linux - 递归地创建文件夹的新 tar 文件,排除另一个 tar 文件中包含的文件

mysql - 将数据 Infile 加载到 mysql 表中

PHP MYSQL : way to list table in ascending order

php - print_r 显示私有(private)变量。为什么?

php - 无法使用php上传文件?

php - chmod、php 和 imageupload