2012-02-26 64 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