PHP
  Home arrow PHP arrow Page 2 - 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 - Handling file uploads using the object-oriented paradigm
    ( Page 2 of 4 )

    Simply put, the first step that I’m going to take concerning the creation of a modular file uploading class with PHP 5 will consist of defining the skeleton of the class in question, while also implementing its pertinent constructor. This way you can grasp how it works more easily.

    Having said that, take a close look at the signature of the following brand new file uploading class, which, driven by my astounding creativity (I’m kidding), I called “FileUploader.” Here it is:


    // define 'FileUploader' class


    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 in the web server

    public function upload(){

    / / code to perform file uploads goes here

    }

    }


    As you can see, the structure of the above “FileUploader” class looks quite simple, since it’s comprised of only two methods: the corresponding constructor and an additional one, called “upload()”, which, for now, hasn’t been concretely implemented.

    Returning to the constructor, you can clearly see that this method accepts the directory in the web server where the pertinent uploaded file will be finally moved as its unique input parameter. In addition, it performs a few other useful initialization tasks, such as checking to see whether or not the inputted directory is valid, and whether or not the MIME type of the target file is allowed.

    In this case, I instructed the class to accept only the most common image MIME types, like JPG and GIF, and a couple of additional ones as well, including “text/plain” and “application/msword.” But this condition can be easily modified to either accept more types or, on the other hand, to refuse some of the valid ones.

    All right, at this point you've hopefully grasped the logic implemented by the constructor of the previous “FileUploader()” class. Therefore, it’s time to implement the only method of this class that remains undefined, that is the one called “upload()”.

    However, to learn the details about how this class method will be implemented in a useful fashion, you’ll have to read the following section. Don’t you worry, since it’s only one click away.



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek