PHP
  Home arrow PHP arrow Page 3 - Defining a Custom Function for File Up...
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 
IBM Rational Software Development Conference
 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

Defining a Custom Function for File Uploaders with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-09

    Table of Contents:
  • Defining a Custom Function for File Uploaders with PHP 5
  • The full source code of the previous file uploading application
  • Putting the file uploading script into the “uploadFile()” custom PHP function
  • Putting the uploadFile() PHP custom function to work

  • 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
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Defining a Custom Function for File Uploaders with PHP 5 - Putting the file uploading script into the “uploadFile()” custom PHP function
    (Page 3 of 4 )

    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


       · Over the course of this fourth part of the series, the file uploading application...
     

       

    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




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