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:
Here is the class file, save it in the same directory as the PHP file that implements it With the name "Upload.php".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"; } ?>
http://scriptplayground.com/public/php/Upload.phps
[Edited on 5/18/2008 by mkeefe]



Reply With Quote
