PHP
  Home arrow PHP arrow Page 2 - Developing a Modular Class For a PHP F...
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 
 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

Developing a Modular Class For a PHP File Uploader
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    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:
      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

    Dell PowerEdge Servers

    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


       · This last part of the series, is aimed at demonstrating how to build a PHP 5 class,...
     

       

    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 1 hosted by Hostway