ImageUploader

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

$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

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

Apart of this, i really like the class!

Thanks for your Feedback

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

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

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

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options