Home arrow PHP arrow Page 3 - Creating an Error Handling Module for a PHP 5 File Uploader

Creating an effective error handling module - PHP

Welcome to the third chapter of the series “Building file uploaders with PHP 5.” Comprised of five approachable tutorials, this series teaches you the fundamentals of constructing file uploading applications with PHP 5. It also complements the corresponding theoretical concepts with numerous hands-on examples. In this third tutorial of the series, I’m going to show you how to incorporate a brand new error checking module into the file uploading PHP script created in the last article.

  1. Creating an Error Handling Module for a PHP 5 File Uploader
  2. Performing file uploads without checking errors
  3. Creating an effective error handling module
  4. Listing the complete source code of the improved file uploading application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
April 02, 2008

print this article



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



// set upload directory (for Windows users)


// set destination of uploaded file



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:



// display error messages when file upload fails

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

case 1:

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


case 2:

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


case 3:

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


case 4:

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


case 6:

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


case 7:

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


case 8:

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






catch(Exception $e){

echo $e->getMessage();



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: