PHP
  Home arrow PHP arrow Page 3 - Developing a Modular Class For a PHP File Uploader
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Developing a Modular Class For a PHP File Uploader
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-04-16


    Table of Contents:
  • Developing a Modular Class For a PHP File Uploader
  • Handling file uploads using the object-oriented paradigm
  • Completing the definition of the FileUploader class
  • Testing the FileUploader class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Developing a Modular Class For a PHP File Uploader - Completing the definition of the FileUploader class
    ( Page 3 of 4 )

    As you saw in the previous section, the only method of the pertinent “FileUploader” class that remains undefined is the one called “upload().” However, as its name suggests, the method should encapsulate all the logic required to upload a selected file to its final location in the web server, as well as check if this process has been performed successfully.

    Therefore, taking into account all of the tasks that must be accomplished by the  “upload()” class method, below I listed its complete definition, which looks like this:


    public function upload(){

    if(move_uploaded_file($this->tmp_name,$this->uploadFile)){

    return true;

    }

    // throw exception according to error number

    switch($this->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;

    }

    }


    In this case, if you had the chance to read the preceding article of this series, you'll find the implementation of the above “upload()” class method quite simple to grasp, since it looks very similar to the “uploadFile()” procedural function defined in that tutorial. In either case, the prior method uses the “move_uploaded_file()” PHP built-in function to transfer the target file to its final location in the web server, assuming that the file upload has been completed successfully.

    On the other hand, if this process fails for whatever reason, an exception will be triggered, indicating the cause of this error. Quite easy to grasp, right?

    Having explained how the previous “upload()” method does its business, I'm now going to list the complete signature of the “FileUploader” class, only this time including the method in question. Here’s how this class now looks:


    class FileUploader{

    private $uploadFile;

    private $name;

    private $tmp_name;

    private $type;

    private $size;

    private $error;

    private $allowedTypes=array
    ('image/jpeg','image/gif','image/png','text/plain','application/ms-word');

    public function __construct($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.');

    }

    foreach($_FILES['userfile'] as $key=>$value){

    $this->{$key}=$value;

    }

    if(!in_array($this->type,$this->allowedTypes)){

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

    }

    $this->uploadFile=$uploadDir.basename($this->name);

    }

    // upload target file to specified location

    public function upload(){

    if(move_uploaded_file($this->tmp_name,$this->uploadFile)){

    return true;

    }

    // throw exception according to error number

    switch($this->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;

    }

    }

    }


    At this moment, you’ll have to agree with me that the above “FileUploader” class is much more useful, while maintaining a high level of modularity. Of course, there’s the possibility that you may want to add some additional logic to some of its existing methods, or even define new ones, but this will depend exclusively on your personal requirements.

    With the previous “FileUploader” class defined completely, it’s time to see how it can be used in the context of a hands-on example. Thus, in the last section of this tutorial, I’ll be setting up this example for you, completing this series on handling file uploads with PHP 5.

    Click on the link below and keep reading. We’re almost done!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek