PHP 无法找到文件,即使它存在

标签 php

我在带有 Apache 2 的 Ubuntu 18.04 上运行 PHP 7.4.9。

我的 PHP 错误日志中出现了这个错误:

"PHP Fatal error: require_once(): Failed opening required 'HTMLPurifier.auto.php' (include_path='(other dirs):/usr/share/php:(other dirs)') in /var/www/testing.php on line 8, referer: http://localhost:8080/testing.php"

所以我检查了文件是否存在于/usr/share/php中:

# ls -lah /usr/share/php
> -rwxrwxrwx   1 www-data www-data  274 Aug 15 15:53 HTMLPurifier.auto.php

所以它就在那里。权限是 777,因为我在尝试解决问题时更改了它们。

然后我在浏览器中访问了一个 phpinfo(); 文件并检查了目录是否在包含路径中并且 open_basedir 是空的:

Directive       Local Value   Master Value
include_path    (other dirs)  .:/usr/share/php
open_basedir    no value      no value

然后我创建了另一个 PHP 文件并写了这个来检查 php 是否可以看到该文件:

<?php

clearstatcache();

$dir = '/usr/share/php/';
$filename = 'HTMLPurifier.auto.php';
$fullpath = $dir . $filename;

var_dump($dir);
var_dump($filename);
var_dump($fullpath);

var_dump(is_dir($dir));

$all_files = scandir($dir);

var_dump($all_files);
var_dump(file_exists($fullpath));
var_dump(file_exists($dir . $all_files[12]));

当我在浏览器中访问它时,我得到:

string(15) "/usr/share/php/" // $dir

string(21) "HTMLPurifier.auto.php" // $filename

string(36) "/usr/share/php/HTMLPurifier.auto.php" // $fullpath

bool(true) // is_dir($dir)

// $all_files
array(40) {
  (other files)
  [12]=> string(21) "HTMLPurifier.auto.php"
}

bool(false) // file_exists($fullpath)

bool(false) // file_exists($dir . $all_files[12])

因此 PHP 可以扫描目录,可以看到那里的文件,但是说它(或目录中的任何其他文件)不存在。我测试了将 /var/www 中的文件传递给 file_exists 并且它有效。我也尝试重命名该文件,但什么也没得到。

什么给了?


编辑:使用 strace php test.php 运行上面的脚本(按照@Progman 的建议)给我:

access("/usr/share/php/HTMLPurifier.auto.php", F_OK) = -1 EACCES (Permission denied)
write(1, "bool(false)\n", 12bool(false)
)           = 12

access("/usr/share/php/HTMLPurifier.auto.php", F_OK) = -1 EACCES (Permission denied)
write(1, "bool(false)\n", 12bool(false)
)           = 12 

所以这是一个权限问题。以 root 身份运行脚本。

$ ls -lah /user/share
drw-rw-rw-  20 root root 4.0K Aug 16 11:52 php

所以我尝试了 sudo chmod 777/usr/share/php 并且脚本有效。少一点(例如 776),我会被拒绝许可。知道我应该如何设置这些权限吗?

最佳答案

在 linux 中,普通用户(如正常工作用户或网络服务器分配的用户)需要目录权限“搜索”,即 --x 或位掩码 1 , 以访问目录内的文件。权限 -r- 或位掩码 2 将只允许您读取目录中的文件名。

要解决此问题,您必须将目录 /usr/share/php 的 chmod 更改为 755

关于PHP 无法找到文件,即使它存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63440067/

相关文章:

php - php 代码中未处理 Html 搜索栏值

PHP 字符串操作 : Extract hrefs

php - 如何使用 AJAX/PHP 将 jQuery 变量写入文本文件?

php - in_array() 总是返回 TRUE

php - 为什么拼接的时候+-操作要加括号?

php - 合并两个 DOMDocument 节点

php - PHP 的 $_SERVER ['REMOTE_ADDR' 的可靠性]

php - 在 PHP 和 MySQL 中创建带循环的嵌套数组

php - 如何使用 Salesforce API 提取选项列表 ID(以及值)?

php - 限制多个函数中的数据库调用次数