我正在嘗試將檔案異步上傳到PHP網站。
PHP負責上傳的代碼是:
ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', 1);
ini_set ('display_startup_errors', 1);
echo 'username = ' . `whoami`."\n";
var_dump($_FILES);
$str_name = $_FILES['file']['name'];
echo '$str_name = '.$str_name."\n";
$str_tmp = $_FILES['file']['tmp_name'];
echo '$str_tmp = '.$str_tmp."\n";
$str_tmpdir = substr($_FILES['file']['tmp_name'], 0, strrpos($_FILES['file']['tmp_name'], '/'));
echo '$str_tmpdir = '.$str_tmpdir."\n\n";
echo 'is_dir($str_tmpdir) = '.(is_dir($str_tmpdir) ? 'true' : 'false')."\n\n";
echo 'is_writable($str_tmp) = '.(is_writable($str_tmp) ? 'true' : 'false')."\n\n";
echo 'ini_get("file_uploads") = '.ini_get('file_uploads')."\n\n";
$location = "/Library/WebServer/Documents/pjamesnorris/img/".$str_name;
echo "\n".'move_uploaded_file("'.$_FILES['file']['tmp_name'].'", "'.$location.'")) = '.(move_uploaded_file($_FILES['file']['tmp_name'], $location) ? 'Success' : 'Failure');
并回傳以下輸出:
username = _www
array(1) {
["file"]=>
array(6) {
["name"]=>
string(17) "DantesInferno.jpg"
["full_path"]=>
string(17) "DantesInferno.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(67) "/Library/WebServer/Documents/pjamesnorris/tmp_file_upload/phpRDWjOh"
["error"]=>
int(0)
["size"]=>
int(217602)
}
}
$str_name = DantesInferno.jpg
$str_tmp = /Library/WebServer/Documents/pjamesnorris/tmp_file_upload/phpRDWjOh
$str_tmpdir = /Library/WebServer/Documents/pjamesnorris/tmp_file_upload
is_dir($str_tmpdir) = true
is_writable($str_tmp) = true
ini_get("file_uploads") = 1
<br />
<b>Warning</b>: move_uploaded_file(/Library/WebServer/Documents/pjamesnorris/img/DantesInferno.jpg): Failed to open stream: Permission denied in <b>/Library/WebServer/Documents/pjamesnorris/php/upload.php</b> on line <b>28</b><br />
<br />
<b>Warning</b>: move_uploaded_file(): Unable to move "/Library/WebServer/Documents/pjamesnorris/tmp_file_upload/phpRDWjOh" to "/Library/WebServer/Documents/pjamesnorris/img/DantesInferno.jpg" in <b>/Library/WebServer/Documents/pjamesnorris/php/upload.php</b> on line <b>28</b><br />
move_uploaded_file("/Library/WebServer/Documents/pjamesnorris/tmp_file_upload/phpRDWjOh", "/Library/WebServer/Documents/pjamesnorris/img/DantesInferno.jpg")) = Failure
這PHP由以下呼叫javascript:
async function uploadFile()
{
let formData = new FormData();
formData.append("file", fileupload.files[0]);
await fetch('../php/upload.php',
{
method: "POST",
body: formData
});
alert('The file has been uploaded successfully.');
}
上面的輸出告訴我它/Library/WebServer/Documents/pjamesnorris/tmp_file_upload是可寫的,它的所有者和權限是:
drwxrwxr-x 2 _www wheel 64 Jun 10 10:35 tmp_file_upload/
其他人似乎已通過將目錄權限設定為更正,0777但似乎我會讓自己面臨安全問題,我已經嘗試過了,但沒有任何效果。
我完全不知道問題是什么/可能是什么,任何幫助將不勝感激!
uj5u.com熱心網友回復:
目標檔案夾的所有者/Library/WebServer/Documents/pjamesnorris/img/也必須是_www!
uj5u.com熱心網友回復:
您的目錄 /Library/WebServer/Documents/pjamesnorris/tmp_file_upload 具有權限: drwxrwxr-x 2 _www wheel 64 Jun 10 10:35 tmp_file_upload/
d 代表一個目錄。第一個 rwx 告訴我們 _www 可以讀取、寫入和搜索目錄。第二個 rwx 告訴你 wheel 組中的任何人都可以讀取、寫入和搜索目錄。最后的 rx 告訴你其他人只能閱讀或搜索。
0777 修復的原因是它允許任何人寫作。你需要找到的是為什么你的軟體不是在用戶_www下運行,也不是由wheel組中的用戶運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491973.html
標籤:javascript php 苹果系统 上传文件
