簡體   English   中英

PHP 找不到文件,即使它存在

[英]PHP can't find file even though it exists

我在 Ubuntu 18.04 和 Apache 2 上運行 PHP 7.4.9。

我的 PHP 錯誤日志出現此錯誤:

“PHP 致命錯誤:require_once():在 /var/www/testing.php 中打開所需的 'HTMLPurifier.auto.php' (include_path='(other dirs):/usr/share/php:(other dirs)') 失敗在第 8 行,引用: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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM