2012-06-19 130 views
0

我不明白爲什麼這不起作用。它應該很容易。PHP - 文件上傳失敗

upload.php的:

$uploaddir = '/usr/share/nginx/www/pitfax/upload/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 

echo "<p>"; 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    echo "File is valid, and was successfully uploaded.\n"; 
} 
else { 
    echo "Upload failed"; 
} 

echo "</p>"; 
echo '<pre>'; 
echo 'Here is some more debugging info:'; 
print_r($_FILES); print "</pre>"; 

形式頁:

<form enctype="multipart/form-data" action="upload.php" method="POST"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> 
    Send this file: <input name="userfile" type="file" /> 
    <input type="submit" value="Send File" /> 
</form> 

錯誤輸出是:

Upload failed 

Here is some more debugging info:Array (
    [userfile] => Array 
     (
      [name] => test.pdf 
      [type] => application/pdf 
      [tmp_name] => /tmp/phptM0p4w 
      [error] => 0 
      [size] => 1287464 
     ) 

) 
  • 網絡服務器:NGINX
  • 權限upload.php:644個
  • 權限upload目錄:755
  • 在SSH與根用戶工作。
  • 編輯:的upload_max_filesize = 2M
  • 編輯:的post_max_size = 8M

另一件事!上傳完成後,我想獲取上傳文件的絕對路徑。 (例如:/usr/share/nginx/www/pitfax/upload/test.pdf

+3

什麼是錯誤日誌? – zerkms

+1

您是否嘗試將上傳目錄設置爲777? –

+0

@LukePittman是的,它沒有奏效。 – xperator

回答

1

如果$ uploadfile值上傳/檢驗.pdf,但你希望它是/usr/share/nginx/www/pitfax/upload/test.pdf比我會懷疑這將是你的問題。

試試這個代碼:

$uploadfile = '/usr/share/nginx/www/pitfax/upload/' . $_FILES['userfile']['name']; 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    echo "File is valid, and was successfully uploaded.\n"; 
} else { 
    print_r ($_FILES); 
} 

編輯:代碼格式。

+0

確定'echo $ uploadfile;'的輸出是正確的。但上傳仍然失敗。輸出爲Array([userfile] => Array([name] => test.pdf [type] => application/pdf [tmp_name] =>/tmp/phpbGRbCg [error] => 0 [size] => 1287464) )' – xperator

+0

當試圖幫助有問題的人時,我被告知這不是一個調試服務。所以我很害怕在這一點上我沒有太多可以做的事情。我會檢查提示的日誌文件(apache錯誤日誌),回顯所有變量,嘗試使用固定的文件名,等等。 –

+0

我再次將上傳目錄的權限更改爲777,並突然上傳工作!謝謝! – xperator