2012-02-26 62 views
0

我有用於在服務器上上傳文件的代碼。但是我得到了由else語句產生的自定義錯誤,但我需要知道爲什麼php無法上傳文件的真正原因。 我得到錯誤2,但我沒有得到實際的消息。 有什麼建議嗎?使用php上傳文件時出現Apear錯誤

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path."/")) { 
      echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded"; 
     } else{ 
      echo "There was an error uploading the file, please try again! ".$_FILES['uploadedfile']['error']; 
     } 
+1

打開的error_reporting。 'error_reporting(E_ALL);'並查看你的日誌。或者打開'display_errors'進行開發。 'ini_set('display_errors',1);' – 2012-02-26 13:51:05

+0

上傳的文件超出了HTML表單中指定的MAX_FILE_SIZE指令。 Thanx – themis 2012-02-26 13:53:48

+0

$ _FILES ['uploadedfile'] ['error']報告的錯誤代碼是什麼? – 2012-02-26 13:54:53

回答

1

你可以試試這個實際出錯

$upload_errors = array(
    UPLOAD_ERR_OK  => "No errors.", 
    UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.", 
    UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.", 
    UPLOAD_ERR_PARTIAL => "Partial upload.", 
    UPLOAD_ERR_NO_FILE  => "No file.", 
    UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.", 
    UPLOAD_ERR_CANT_WRITE => "Can't write to disk.", 
    UPLOAD_ERR_EXTENSION  => "File upload stopped by extension.", 
    UPLOAD_ERR_EMPTY  => "File is empty." // add this to avoid an offset 
); 
    // error: report what PHP says went wrong 
    $err = $upload_errors[$_FILES['uploadedfile']['error']]; 

    echo $err; 
+0

我一直在尋找這個真實的。我只是在我的控制器中創建了這個功能。所以沒有辦法,PHP可以產生消息,只有錯誤代碼,然後使用這些消息的自定義函數。 – themis 2012-02-26 14:27:22

1

林不知道爲什麼你對目標參數$target_path."/"的末尾添加目錄分隔符。

刪除它或用$target_path.basename($_FILES['uploadedfile']['name'])替換應該有效。

<?php 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again! ".$_FILES['uploadedfile']['error']; 
} 
?> 
+0

thanx,但它適用於小文件。我的問題是,我需要得到實際的錯誤信息是在運行時displayd – themis 2012-02-26 14:17:19