The Web is getting smarter. We help you keep up.
I’ve recently released a small Image Uploader class for PHP. You can download it from PHPClasses. The class is very simple, and only does one thing – as all good classes do. It processes images uploaded via a web Form, resizes and saves them. It can handle JPG, GIF & PNG files, and saves all files in JPG format.
See below for a usage example:
<?php
include('ImageUploader.php');
//The array of sizes the file should produce. The key (ie, full) will be appended to the filename
$sizesArray = array (
'full' => array(640,480),
'thumb' => array(150, 112)
);
$targetDirectory = ‘images’; //Where to save the file
$newFilename = ‘MattTest’; //The new filename. Any periods, or text following a period, is stripped
//When uploading a file with a POST form, it is saved to the $_FILES array
if (isset($_FILES['image'])) {
$imgUploader = new ImageUploader($_FILES[‘image’], $sizesArray, $targetDirectory, $newFilename);
//Make sure everything is operating properly before attempting to save
if ($img->getStatus() == ImageUploader::CAN_CONTINUE) {
$img->saveFiles();
}
//Create a little message to show on screen with the results of the upload
if ($img instanceof ImageUploader) { //This prevents warnings the first time through the form
switch($img->getStatus()) {
case ImageUploader::SUCCESS:
print '<span style="color:#FF0000">Image Uploaded Successfully</span>';
break;
case ImageUploader::INVALID_FILE_TYPE:
print '<span style="color:#FF0000">Invalid File Type</span>';
break;
case ImageUploader::UNABLE_TO_CREATE_IMG:
print '<span style="color:#FF0000">Images could not be saved</span>';
break;
case ImageUploader::INVALID_SIZES:
print '<span style="color:#FF0000">Invalid target sizes</span>';
break;
case ImageUploader::NO_WRITE_PERMISSIONS:
print '<span style="color:#FF0000">Cannot write to target directory</span>';
break;
case ImageUploader::FILE_SAVE_ERROR:
print '<span style="color:#FF0000">Images could not be saved</span>';
break;
}
print '<br />';
//Show the newly uploaded image
if ($img->getStatus() == ImageUploader::SUCCESS) {
$files = $img->getFilenames();
foreach ($files as $file) {
print "<img src='$file' /><br />";
}
}
}
<form method="POST" action="index.php" enctype="multipart/form-data">
<input type="file" name="image" />
<br />
<input type="submit" value="Load Images" />
</form>
?>This code is free for use in any projects you have, and free to distribute. If you do distribute, please keep the copyright and author information intact.
If you have any questions or comments, please leave them here. I’d also like to see links to anywhere this is used.
ImageUploader
Submitted by mw (not verified) on Wed, 09/17/2008 - 16:27.$newFilename not functional.
Modern versions of Microsoft Windows OS (2000 and newer) understands /, no conversion to \ needed
If $newFilename would be functional, this class can be handy!
ImageUploader
Submitted by mw (not verified) on Wed, 09/17/2008 - 17:56.if "validateFilename($fileroot)" is not used (changed
if (isset($fileroot) && $this->validateFilename($fileroot)) {
to
if (isset($fileroot)) {
it works.
Maybe regular expression needs a overhaul.
Also:
$farray = preg_split('/\./',$fileroot);
could be faster:
$farray = explode(".",$fileroot);
Apart of this, i really like
Submitted by Anonymous (not verified) on Wed, 09/17/2008 - 18:08.Apart of this, i really like the class!
Thanks for your Feedback
Submitted by Matt on Wed, 09/24/2008 - 01:52.Thank you for your feedback. When I get back in to this to make changes - if I do - I'll definitely take them in to consideration.
Multiple uploads
Submitted by Nicholas Pufal (not verified) on Wed, 12/17/2008 - 12:01.First of all, thanks a lot for this. I found pretty simple the code, and still efficient.
I just have one question about it. How could I make it to support multiple uploads?
I made my form came up with an array of fields. So I used foreach on it, something as the following:
<?php $file = $_FILES['campo_upload']; Foreach ($file['name'] as $pic_name) { $img = new ImageUploader($pic_name, $sizes, 'images',''); if ($img->getStatus() == ImageUploader::CAN_CONTINUE) { $img->saveFiles(); } }?>But I keep getting "Invalid File Type". I checked the mime type that is returned from it, and it should work, because they are all image/jpeg.
Any ideas over this?
I really appreciate any help.
Thanks!
Multiple Files
Submitted by Matt on Thu, 12/18/2008 - 15:03.When you want to do multiple files, you actually have to pass the full array from $_FILES in to the object.
So you would do
<?php foreach ($_FILES as $file) { $img = new ImageUploader($file,$sizes, 'images,');...?>Make sense?
And now I need to figure out why the Code input filter isn't working for comments...
Post new comment