PHP
  Home arrow PHP arrow Page 3 - Creating an Error Handling Module for a PHP 5 File Uploader
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Creating an Error Handling Module for a PHP 5 File Uploader
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-04-02


    Table of Contents:
  • Creating an Error Handling Module for a PHP 5 File Uploader
  • Performing file uploads without checking errors
  • Creating an effective error handling module
  • Listing the complete source code of the improved file uploading application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating an Error Handling Module for a PHP 5 File Uploader - Creating an effective error handling module
    ( Page 3 of 4 )

    As I said in the previous section, the $_FILES array stores a predefined set of error codes when a file upload has been performed. This facilitates the development of an error checking mechanism for keeping track of all the possible failures that might occur during a specific file upload.

    However, this will be better understood through an example. Please take a look at the signature of the following file uploading script, which implements a rudimentary error checking module capable of triggering different exceptions in accordance with the type of error caused when a file has been uploaded to the web server.

    The corresponding code sample is as follows:


    // example on checking file uploading errors with the $_FILES array


    try{

    if($_POST['send']){

    // set upload directory (for Windows users)

    $uploadDir='C:uploaded_files';

    // set destination of uploaded file

    $uploadFile=$uploadDir.basename($_FILES['userfile']['name']);

    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile)){

    echo 'The target file was successfully uploaded!<br />Name of uploaded file: '.$_FILES
    ['userfile']['name'].'.<br />MIME type of uploaded file: '.$_FILES['userfile']['type'].'.<br />Size of
    uploaded file: '.$_FILES['userfile']['size'].' bytes.<br />Temporary name of uploaded file:
    '.$_FILES['userfile']['tmp_name'];

    }

    else{

    // display error messages when file upload fails

    switch ($_FILES['userfile']['error']){

    case 1:

    throw new Exception('Target file exceeds maximum allowed size.');

    break;

    case 2:

    throw new Exception('Target file exceeds the MAX_FILE_SIZE value specified on the upload form.');

    break;

    case 3:

    throw new Exception('Target file was not uploaded completely.');

    break;

    case 4:

    throw new Exception('No target file was uploaded.');

    break;

    case 6:

    throw new Exception('Missing a temporary folder.');

    break;

    case 7:

    throw new Exception('Failed to write target file to disk.');

    break;

    case 8:

    throw new Exception('File upload stopped by extension.');

    break;

    }

    }

    }

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Things are becoming quite interesting at this point! As you can see, the above file uploading script has now been provided with the ability to trigger different exceptions according to the value stored in the $_FILES['userfile]['error'] array element.

    Obviously, the error codes stored in the array element aren't simply an invention of mine; they're automatically generated by the PHP engine to keep track of different failures that might arise when a file uploading process is being performed via an HTTP POST request.

    Based on this handy error handling mechanism, it's possible to determine what went wrong during a specific file upload. For instance, the size of the selected file might exceed an allowed limit, or may have an invalid extension, etc. In this case, each possible failure can be easily handled via the $_FILES PHP array, as demonstrated by the previous PHP script.

    All right, at this level, you've hopefully learned how to incorporate a basic error handling module into the file uploading application developed previously, which helps track different failures that might occur when a specific file is being uploaded to the web server.

    Nonetheless, the previous file uploading script looks rather disjointed if it's not accompanied by the corresponding web form that allows users to browse the files in their client machines. Thus, in the final section of this tutorial, I'll be listing the complete source code of this recently-improved file uploading application, including the web form in question.

    So, take a deep breath and read the following lines. 



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek