PHP Multiple Image Upload Script / User Image Upload - That actually works!

June 4, 2009    Image upload Multiple image upload script php

For quite a long time now ive been seeing lots of image upload scripts which boast to be able to upload multiple images at a time! 90% dont work.

So heres one I created and I know works. Firstly the below code is the functions to handle the images,  you do not need to touch anything here, just make sure its on your page.

* Dave Hewards image script

function resize($img, $thumb_width, $newfilename){

//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
    trigger_error("GD is not loaded", E_USER_WARNING);
    return false;

//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);

switch ($image_type)
    case 1: $im = imagecreatefromgif($img); break;
    case 2: $im = imagecreatefromjpeg($img);  break;
    case 3: $im = imagecreatefrompng($img); break;
    default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;

/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;

/*** calculate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);

     $thumb_height = round($thumb_width * $aspect_ratio);

$newImg = imagecreatetruecolor($thumb_width, $thumb_height);

/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
    imagealphablending($newImg, false);
    $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);

imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);

//Generate the file, and rename it to $newfilename
switch ($image_type)
    case 1: imagegif($newImg,$newfilename); break;
    case 2: imagejpeg($newImg,$newfilename);  break;
    case 3: imagepng($newImg,$newfilename); break;
    default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;

return $newfilename;

The next part is the important part which you will need to change some variables on to customise them for your own site.

//This stuff is outside of the function. It operates with our images
    $imgNumb=1; //This the "pointer" to images
    $DestinationDir="/sites/";  //Place the destination dir here
    $ThumbDir="/sites/";  //Place the thumb dir here

    $Unique=microtime(); // We want unique names, right?

    $finalimage = resize($_FILES["img$imgNumb"][tmp_name], 199, $destination);
    //use the filename variable below to record the image name entered so that you can then use this variable to update your database if

    $filename = md5($Unique).".jpg";
    $field = 'img'.$imgNumb;

    //mysql update your database fields

    mysql_query("update profile set $field = '$filename' where playerid='$id'");



} while($_FILES["img$imgNumb"][name]);

$message = "Images uploaded!";

}// end

Finally you will need a little bit of HTML code for your form for the user to submit his/her new images to the site.

Change Profile Images:
Image 1:
Image 2:
Image 3:

This code is tried and tested and it works so please enjoy and comment if you have some suggestions for ways to improve it further.

blog comments powered by Disqus