2015-01-16 30 views
2

這是怎麼回事?PHP FileUpload錯誤

<form method="post" enctype="multipart/form-data" action="php/api/member_settings_profile_avatar.php"> 
    <input type="file" name="settings_choose_avatar" id="settings_choose_avatar"> 
    <input type="submit" value="Upload" /> 
</form> 

PHP/API/member_settings_profile_avatar.php,2號線:

move_uploaded_file($_FILES["settings_choose_avatar"]["tmp_name"], "img/test.png"); 

我得到在提交有效的PNG文件以下錯誤:

Warning: move_uploaded_file(img/test.png): failed to open stream: No such 
file or directory in /customers/4/1/a/mysitenamehere.com/httpd.www/php/api 
/member_settings_profile_avatar.php on line 2 Warning: move_uploaded_file(): 
Unable to move '/customers/4/1/a/mysitenamehere.com/tmp/phpeN9wyk' to 
'img/test.png' in /customers/4/1/a/mysitenamehere.com/httpd.www/php/api 
/member_settings_profile_avatar.php on line 2 

是否有問題我的主人還是什麼?

+3

路徑是否存在?嘗試上傳之前是否創建了路徑(mkdir)?路徑是否正確? –

+1

如果對@TopQuestions問題的答案都是肯定的,你是否檢查文件夾中的is_writable? –

+1

你確定你的路徑是否正確,你想將圖像存儲在'/ php/api/img'文件夾中還是你的意思是'/ img'(都是相對於web-root的)? – jeroen

回答

1

問題是目標的路徑應該是相對於網站的根。您可以使用$_SERVER['DOCUMENT_ROOT']並將路徑添加到要上傳文件的文件夾中。

move_uploaded_file($_FILES["settings_choose_avatar"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . "/img/test.png"); 

如果您當前的php文件位於網站的根文件夾中,它將工作。

+0

這樣做,謝謝! – david

+0

不客氣。 – Skatox

相關問題