Results 1 to 4 of 4

Thread: File Upload class in PHP

  1. #1
    Programming Ninja mkeefe's Avatar
    Join Date
    Feb 2003
    Location
    Boston
    Posts
    7,794

    File Upload class in PHP

    There has been some talk about uploading files in PHP. Well I decided that this class (I use a modified version from this) could be useful.

    It lets you keep all the uploading logic out of your file and only takes a couple of lines to get things going.

    Features:

    - multiple file upload
    - protection for overwrites
    - security on the filename
    - easy to implement design
    - class based to be re-used
    - error trapping

    How to use:

    Code:
    <?php
    
    // import the required upload class
    require 'Upload.php';
    
    // set a directory where the files would be uploaded to
    $imageDir = 'images/uploads/';
    
    // Make the directory, if it
    // doesn't already exist
    if(!is_dir($imageDir))
    {
    	mkdir($imageDir);
    	chmod($imageDir, 0777);
    }
    
    // grab the images from the $_FILE array
    $images = $_FILES['images'];
    
    // create a new instance of the Upload class
    $upload = new Upload($imageDir);
    
    // kick off the uploading process
    $uploadResult = $upload->uploadFiles($images);
    
    // grab the uploaded files
    $uploads = $upload->getUploaded();
    
    // uncomment this line to see the raw upload data
    //print_r($uploads);
    
    // loop through all the uploaded files, print out 
    // the name. In a more complete application you 
    // could insert the entry into the database.
    foreach($uploads as $file)
    {
    	print "File Name: " . $file['name'] . "<br />\n";	
    }
    
    ?>
    Here is the class file, save it in the same directory as the PHP file that implements it With the name "Upload.php".

    http://scriptplayground.com/public/php/Upload.phps

    [Edited on 5/18/2008 by mkeefe]

  2. #2
    Barrista
    Join Date
    Oct 2006
    Location
    Thailand
    Posts
    1,220
    Awesome mkeefe!!! Bookmarked and noted for future use!

    Thanks!

  3. #3
    Programming Ninja mkeefe's Avatar
    Join Date
    Feb 2003
    Location
    Boston
    Posts
    7,794
    Cool, glad you like it. I have some other scripts I should upload.

  4. #4
    Regular
    Join Date
    Jan 2003
    Location
    California
    Posts
    403
    ow, nice this will be helpful. thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •