Разъяснение сообщений об ошибках

Начиная с PHP 4.2.0, PHP возвращает код ошибки наряду с другими атрибутами принятого файла. Он расположен в массиве, создаваемом PHP при загрузке файла, и может быть получен при обращении по ключу ['error']. Говоря другими словами, код ошибки можно найти в переменной $_FILES['userfile']['error'].

UPLOAD_ERR_OK

Значение: 0; Ошибок не возникало, файл был успешно загружен на сервер.

UPLOAD_ERR_INI_SIZE

Значение: 1; Размер принятого файла превысил максимально допустимый размер, который задан директивой upload_max_filesize конфигурационного файла php.ini.

UPLOAD_ERR_FORM_SIZE

Значение: 2; Размер загружаемого файла превысил значение MAX_FILE_SIZE, указанное в HTML-форме.

UPLOAD_ERR_PARTIAL

Значение: 3; Загружаемый файл был получен только частично.

UPLOAD_ERR_NO_FILE

Значение: 4; Файл не был загружен.

Замечание: Константы были добавлены в PHP 4.3.0.



Разъяснение сообщений об ошибках
rjg4013 at rit dot edu
06-Jul-2006 02: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 03: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
26-Sep-2005 03:05
if post is greater thanpost_max_size set in php.ini

$_FILES and $_POST will return empty
hans at lintoo dot dk
15-Jul-2005 01: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 07: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
15-Feb-2005 07: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
27-Jan-2005 04: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 06: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
03-Nov-2004 07: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
}

< Загрузка файлов на серверНаиболее распространенные ошибки>
 Last updated: Mon, 14 Nov 2005