PHP
  Home arrow PHP arrow Page 3 - Creating an Error Handling Module for ...
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
PHP

Creating an Error Handling Module for a PHP 5 File Uploader
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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
     
     
    The Best Selling PC Migration Utility.
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    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


       · File uploads, as other tasks performed by users, require to check input data....
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway