note 107295 modified in features.file-upload.errors by danbrown

From: 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 &#34;error_reporting&#34;) about &#34;Length parameter must be greater than 0&#34;&#13;&#10;especially if you are parsing this as xml.&#13;&#10;So in order to handle this error from here you may do the next:&#13;&#10;&#60;?php&#13;&#10;$file = $_FILES[&#39;file&#39;];&#13;&#10;$error_text = true; // Show text or number&#13;&#10;define(&#34;UPLOAD_ERR_EMPTY&#34;,5);&#13;&#10; if($file[&#39;size&#39;] == 0 &#38;&#38; $file[&#39;error&#39;] == 0){&#13;&#10; $file[&#39;error&#39;] = 5;&#13;&#10; }&#13;&#10; $upload_errors = array(&#13;&#10; UPLOAD_ERR_OK&#9;&#9;=&#62; &#34;No errors.&#34;, &#13;&#10; UPLOAD_ERR_INI_SIZE&#9;=&#62; &#34;Larger than upload_max_filesize.&#34;,&#13;&#10; UPLOAD_ERR_FORM_SIZE&#9;=&#62; &#34;Larger than form MAX_FILE_SIZE.&#34;,&#13;&#10; UPLOAD_ERR_PARTIAL&#9;=&#62; &#34;Partial upload.&#34;,&#13;&#10; UPLOAD_ERR_NO_FILE&#9;&#9;=&#62; &#34;No file.&#34;,&#13;&#10; UPLOAD_ERR_NO_TMP_DIR&#9;=&#62; &#34;No temporary directory.&#34;,&#13;&#10; UPLOAD_ERR_CANT_WRITE&#9;=&#62; &#34;Can&#39;t write to disk.&#34;,&#13;&#10; UPLOAD_ERR_EXTENSION &#9;=&#62; &#34;File upload stopped by extension.&#34;,&#13;&#10; UPLOAD_ERR_EMPTY&#9;&#9;=&#62; &#34;File is empty.&#34; // add this to avoid an offset&#13;&#10; );&#13;&#10; // error: report what PHP says went wrong&#13;&#10; $err = ($error_text) ? $upload_errors[$file[&#39;error&#39;]] : $file[&#39;error&#39;] ;&#13;&#10; header(&#34;Content-Type: text/xml&#34;);&#13;&#10; echo &#39;&#60;errors&#62;&#13;&#10; &#60;error&#62;&#39;.&#13;&#10; $err .&#39;&#13;&#10; &#60;/error&#62;&#13;&#10; &#60;/errors&#62;&#13;&#10; &#39;;&#13;&#10; // Add your code to prevent fread() to run, either from here or with a previous &#34;if&#34;&#13;&#10;?&#62; --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

« previous php.notes (#187896) next »