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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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'){

if(!is_dir($uploadDir)){

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

}

if(!count($_FILES)){

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

}

if(!in_array($_FILES['userfile']['type'],array
('image/jpeg','image/gif','image/png','text/plain','application/msword'))){

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

}

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

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

return true;

}

// throw exception according to error number

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;

}

}


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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: