Programming Tutorials

Error: Length parameter must be greater than 0

By: capitanqueso in PHP Tutorials on 2012-12-05  

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"  
?>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

Latest Articles (in PHP)