note 107295 modified in features.file-upload.errors by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 21:29:41 +0000 |
| Subject: | note 107295 modified in features.file-upload.errors by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187896@lists.php.net to get a copy of this message | ||
If you ever try to upload an empty file using fread(), php may show a nasty warning (according to
your "error_reporting") about "Length parameter must be greater than
0" especially if you are parsing this as xml. So in order
to handle this error from here you may do the
next: <?php $file =
$_FILES['file']; $error_text = true; // Show text or
number define("UPLOAD_ERR_EMPTY",5);
if($file['size'] == 0 && $file['error'] ==
0){ $file['error'] = 5;
} $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 =
($error_text) ? $upload_errors[$file['error']] : $file['error']
; header("Content-Type: text/xml"); echo
'<errors>
<error>'. $err .'
</error>
</errors> '; // Add your code to
prevent fread() to run, either from here or with a previous
"if" ?>
--was--
<?php
/*
If you ever try to upload an empty file using fread(), php may show a nasty warning (according to
your "error_reporting") about "Length parameter must be greater than 0"
especially if you are parsing this as xml.
So in order to handle this error from here you may do the next:
*/
$file = $_FILES['file'];
$error_text = true; // Show text or number
define("UPLOAD_ERR_EMPTY",5);
if($file['size'] == 0){
$file['error'] = 5;
}
$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 = ($error_text) ? $upload_errors[$file['error']] : $file['error'] ;
header("Content-Type: text/xml");
echo '<errors>
<error>'.
$err .'
</error>
</errors>
';
// Add your code to prevent fread() to run, either from here or with a previous "if"
?>
http://php.net/manual/en/features.file-upload.errors.php