错误信息说明

从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码。该代码可以在文件上传时生成的文件数组中的 error 字段中被找到,也就是 $_FILES['userfile']['error']

UPLOAD_ERR_OK

其值为 0,没有错误发生,文件上传成功。

UPLOAD_ERR_INI_SIZE

其值为 1,上传的文件超过了 php.iniupload_max_filesize 选项限制的值。

UPLOAD_ERR_FORM_SIZE

其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。

UPLOAD_ERR_PARTIAL

其值为 3,文件只有部分被上传。

UPLOAD_ERR_NO_FILE

其值为 4,没有文件被上传。

UPLOAD_ERR_NO_TMP_DIR

其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。

UPLOAD_ERR_CANT_WRITE

其值为 7,文件写入失败。PHP 5.1.0 引进。

注: 以上值在 PHP 4.3.0 之后变成了 PHP 常量。


add a note add a note User Contributed Notes
rjg4013 at rit dot edu
07-Jul-2006 05:53
In response to Hans:  The problem with your switch statement is that it never allows for no errors, i.e. the UPLOAD_ERR_OK val.  So to still allow room for further errors, yet notice a no-error case, this needs to be addressed:

<?php
switch ($filearray["error"]) {
   case
UPLOAD_ERR_OK:
       break;
   case
UPLOAD_ERR_INI_SIZE:
      
throw new Exception("The uploaded file exceeds the upload_max_filesize directive (".ini_get("upload_max_filesize").") in php.ini.");
       break;
   case
UPLOAD_ERR_FORM_SIZE:
      
throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");
       break;
   case
UPLOAD_ERR_PARTIAL:
      
throw new Exception("The uploaded file was only partially uploaded.");
       break;
   case
UPLOAD_ERR_NO_FILE:
      
throw new Exception("No file was uploaded.");
       break;
   case
UPLOAD_ERR_NO_TMP_DIR:
      
throw new Exception("Missing a temporary folder.");
       break;
   case
UPLOAD_ERR_CANT_WRITE:
      
throw new Exception("Failed to write file to disk");
       break;
   default:
      
throw new Exception("Unknown File Error");
}
?>
-
20-Jan-2006 07:26
Thank you stephen, that was very helpful ;-)

So, to repeat it for all, check your php.ini,

post_max_size

should be bigger than

upload_max_filesize

, otherwise you will not be able to report the correct error in case of a too big upload ! Also check the max-execution-time (upload-time could be added to execution-time).
stephen at poppymedia dot co dot uk
27-Sep-2005 06:05
if post is greater thanpost_max_size set in php.ini

$_FILES and $_POST will return empty
hans at lintoo dot dk
16-Jul-2005 04:18
or perhaps this:
<?php
switch ($filearray["error"]) {
   case
UPLOAD_ERR_INI_SIZE:
      
throw new Exception("The uploaded file exceeds the upload_max_filesize directive (".ini_get("upload_max_filesize").") in php.ini.");
   break;
   case
UPLOAD_ERR_FORM_SIZE:
      
throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");
   break;
   case
UPLOAD_ERR_PARTIAL:
      
throw new Exception("The uploaded file was only partially uploaded.");
   break;
   case
UPLOAD_ERR_NO_FILE:
      
throw new Exception("No file was uploaded.");
   break;
   case
UPLOAD_ERR_NO_TMP_DIR:
      
throw new Exception("Missing a temporary folder.");
   default:
      
throw new Exception("An unknown file upload error occured");
}
?>
adam at gotlinux dot us
27-May-2005 10:28
This is probably useful to someone.
<?
array(
      
0=>"There is no error, the file uploaded with success",
      
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
      
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
      
3=>"The uploaded file was only partially uploaded",
      
4=>"No file was uploaded",
      
6=>"Missing a temporary folder"
);
?>
sysadmin at cs dot fit dot edu
16-Feb-2005 11:13
I noticed that on PHP-4.3.2 that $_FILES can also not be set if the file uploaded exceeds the limits set by upload-max-filesize in the php.ini, rather than setting error $_FILES["file"]["error"]
krissv at ifi.uio.no
28-Jan-2005 08:11
When $_FILES etc is empty like Dub spencer says in the note at the top and the error is not set, that might be because the form enctype isnt sat correctly. then nothing more than maybe a http server error happens.

enctype="multipart/form-data" works fine
Dub Spencer
26-Nov-2004 10:56
Upload doesnt work, and no error?

actually, both $_FILES and $_REQUEST in the posted to script are empty?

just see, if  "post_max_size" is lower than the data you want to load.

in the apache error log, there will be an entry like "Invalid method in request". and in the access log, there will be two requests: one for the POST, and another that starts with all "----" and produces a 501.
tyler at fishmas dot org
04-Nov-2004 11:08
In regards to the dud filename being sent, a very simple way to check for this is to check the file size as well as the file name.  For example, to check the file size simple use the size attribute in your file info array:

if($_FILES["file_id"]["size"]  == 0)
{
         ...PROCESS ERROR
}