Home arrow PHP arrow Page 3 - Defining a Custom Function for File Uploaders with PHP 5

Putting the file uploading script into the “uploadFile()” custom PHP function - PHP

In the next few lines I’m going to show you how to wrap the file uploading application built in the previous article of the series into a single custom PHP function. This will turn it into a more maintainable and reusable piece of code.

  1. Defining a Custom Function for File Uploaders with PHP 5
  2. The full source code of the previous file uploading application
  3. Putting the file uploading script into the “uploadFile()” custom PHP function
  4. Putting the uploadFile() PHP custom function to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
April 09, 2008

print this article



True to form, encapsulating the previous file uploading script into a single custom PHP function is a straightforward process that can be performed without major problems, since most of the pertinent business logic has already been implemented before.

However, let me go one step further and show you the signature of this brand new user-defined PHP function, called “uploadFile().” It looks like this:

// define 'uploadFile()' function

function uploadFile($uploadDir='C:uploaded_files'){


throw new Exception('Invalid upload directory.');



throw new Exception('Invalid number of file upload parameters.');



throw new Exception('Invalid MIME type of target file.');




return true;


// throw exception according to error number


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.');




As you can see, the above “uploadFile()” PHP function encapsulates all the logic required to upload a selected file to the web server within its structure. And it takes up the directory in the web server as its unique input parameter - where the file in question will be moved.

Based on this concept, the function checks to see if this directory is valid or not, and then uses “move_uploaded_file()”, a PHP built-in function, to complete the pertinent file uploading process. In addition, any error that might occur when these tasks are performed will trigger different exceptions that can be easily caught by a conventional “try-catch()” block. Quite simple to understand, right?

Well, at this point I’m pretty sure that you've grasped how the previous “uploadFile()” does its thing. So I'm going to demonstrate how it can be used in the context of a practical example. You will see how useful it can be, when it comes to uploading a file via PHP.

This hands-on demonstration will take place in the next section, so click on the link below and keep reading.

>>> 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: