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.
- 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".
// import the required upload class
// set a directory where the files would be uploaded to
$imageDir = 'images/uploads/';
// Make the directory, if it
// doesn't already exist
// 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
// 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";
[Edited on 5/18/2008 by mkeefe]