Error: Length parameter must be greater than 0

By: capitanqueso Viewed: 153254 times  Printer Friendly Format    

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
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>
$err .'
// Add your code to prevent fread() to run, either from here or with a previous "if"

Most Viewed Articles (in PHP )

Latest Articles (in PHP)

Comment on this tutorial