Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

File Upload


Published on

  • Be the first to comment

File Upload

  1. 1. CSC 2720 Building Web Applications PHP File Upload
  2. 2. Handling File Uploads in PHP <ul><li>PHP must run with the right settings. </li></ul><ul><ul><li>You may need to change the PHP configuration file php.ini . </li></ul></ul><ul><li>A temporary storage directory must exists with the correct permissions. </li></ul><ul><ul><li>Uploaded files are stored here temporarily; the files may be removed from here when your PHP script finishes processing the HTTP request. </li></ul></ul><ul><li>The final storage directory must exists with the correct permissions. </li></ul><ul><ul><li>You have to write codes to move the uploaded files from the temporary directory to another directory. </li></ul></ul>
  3. 3. Configuring php.ini <ul><li>file_uploads </li></ul><ul><ul><li>Enable/disable PHP support for file uploads </li></ul></ul><ul><li>max_input_time </li></ul><ul><ul><li>Indicates how long, in seconds, a PHP script is allowed to receive input </li></ul></ul><ul><li>post_max_size </li></ul><ul><ul><li>Size, in bytes, of the total allowed POST data </li></ul></ul><ul><li>upload_tmp_dir </li></ul><ul><ul><li>Indicates where uploaded files should be temporarily stored </li></ul></ul><ul><li>upload_max_filesize </li></ul><ul><ul><li>Size, in bytes, of the largest possible file upload allowed </li></ul></ul>
  4. 4. HTTP File Upload ( RFC 1867 ) <ul><li>Set attribute METHOD=&quot;POST&quot; </li></ul><ul><li>Set attribute ENCTYPE=&quot;multipart/form-data&quot; </li></ul><ul><li>Use INPUT element with TYPE=&quot;file&quot; to create a file upload control (one control per file) </li></ul><ul><li>Hidden input field MAX_FILE_SIZE recommends to the web client the limit of the size of the uploaded file. </li></ul><form action=&quot;file_upload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; > <input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;30000&quot; /> <input type=&quot;file&quot; name=&quot;upload&quot; /> <input type=&quot;submit&quot; value=&quot;Upload&quot; /> </form>
  5. 5. POST register.jsp HTTP/1.1 Host: hi/iq User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8, video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------29772313742745 Content-Length: 452 -----------------------------29772313742745 Content-Disposition: form-data; name=&quot;name&quot; J.Doe -----------------------------29772313742745 Content-Disposition: form-data; name=&quot;email&quot; [email_address] -----------------------------29772313742745 Content-Disposition: form-data; name=&quot;file-upload&quot;; filename=&quot;test.txt&quot; Content-Type: text/plain test data with some high ascii: ¿Como estás? -----------------------------29772313742745-- An example of data sent via POST method with enctype=&quot;multipart/form-data&quot; (Copied from )
  6. 6. The $_FILES Array The error code associated with any problem. error The temporary filename of the uploaded file as it was stored on the server. tmp_name The size of the uploaded file in bytes. size The MIME type of the file, as provided by the browser. type The original name of the file (as it was on the user's computer). name Meaning Index
  7. 7. Processing the uploaded items // &quot;upload&quot; is the name assigned to the input element, as in // <input type=&quot;file&quot; name=&quot;upload&quot; /> if (isset( $_FILES['upload'] )) { if ( $_FILES['upload']['error'] > 0)) { // File upload fails. See next slide for detailed info about the // meaning of the error code. } else { // e.g., only allows JPEG image files to be uploaded // Note: This is not a complete list of MIME types for JPEG images $allowed = array('image/jpeg', 'image/jpg'); // Continue next page …
  8. 8. Processing the uploaded items ( … continue) if (in_array( $_FILES['upload']['type'] , $allowed)) { $tmp = $_FILES['upload']['tmp_name'] ; $dst = &quot;C:/uploads/{ $_FILES['upload']['name'] }&quot;; if ( move_upload_file ($tmp, $dst)) { // Success ! } } } // End of else // Manually delete the temporary uploaded file if // it still exists $tmp = $_FILES['upload']['tmp_name'] ; if (file_exists($tmp) && is_file($tmp)) unlink($tmp); } Note: move_uploaded_file() will overwrite an existing file without warning.
  9. 9. File Upload Error Messages Explained <ul><li>UPLOAD_ERR_OK </li></ul><ul><ul><li>Value: 0; There is no error, the file uploaded with success. </li></ul></ul><ul><li>UPLOAD_ERR_INI_SIZE </li></ul><ul><ul><li>Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini . </li></ul></ul><ul><li>UPLOAD_ERR_FORM_SIZE </li></ul><ul><ul><li>Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. </li></ul></ul><ul><li>UPLOAD_ERR_PARTIAL </li></ul><ul><ul><li>Value: 3; The uploaded file was only partially uploaded. </li></ul></ul><ul><li>UPLOAD_ERR_NO_FILE </li></ul><ul><ul><li>Value: 4; No file was uploaded. </li></ul></ul><ul><li>UPLOAD_ERR_NO_TMP_DIR </li></ul><ul><ul><li>Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. </li></ul></ul><ul><li>UPLOAD_ERR_CANT_WRITE </li></ul><ul><ul><li>Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0. </li></ul></ul><ul><li>UPLOAD_ERR_EXTENSION </li></ul><ul><ul><li>Value: 8; File upload stopped by extension. Introduced in PHP 5.2.0. </li></ul></ul><ul><li>Source: </li></ul>
  10. 10. References and Related Resources <ul><li>PHP: Filesystem Functions </li></ul><ul><ul><li>e.g., </li></ul></ul><ul><ul><ul><li>mkdir() – Create a directory </li></ul></ul></ul><ul><ul><ul><li>filesie() – Get the size of a file </li></ul></ul></ul><ul><ul><ul><li>copy() – Copy a file </li></ul></ul></ul><ul><ul><ul><li>rename() – Rename a file or folder </li></ul></ul></ul><ul><ul><ul><li>filemtime() – Get file modification time </li></ul></ul></ul><ul><ul><li> </li></ul></ul><ul><li>PHP: Handling File Uploads </li></ul><ul><ul><li> </li></ul></ul><ul><li>PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition) , by Larry Ullman. Peachpit Press, 2007 (pp 302-15) </li></ul>