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

June 04, 2009

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, $thumbwidth, $newfilename){ $maxwidth=$thumb_width;

//Check if GD extension is loaded if (!extensionloaded(‘gd’) && !extensionloaded(‘gd2’)) { triggererror(“GD is not loaded”, EUSER_WARNING); return false; }

//Get Image size info list($widthorig, $heightorig, $image_type) = getimagesize($img);

switch ($imagetype) { case 1: $im = imagecreatefromgif($img); break; case 2: $im = imagecreatefromjpeg($img);  break; case 3: $im = imagecreatefrompng($img); break; default:  triggererror(‘Unsupported filetype!’, EUSERWARNING);  break; }

/* calculate the aspect ratio */ $aspectratio = (float) $heightorig / $width_orig;

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

while($thumbheight>$maxwidth) { $thumbwidth-=10; $thumbheight = round($thumbwidth * $aspectratio); }

$newImg = imagecreatetruecolor($thumbwidth, $thumbheight);

/* Check if this image is PNG or GIF, then set if Transparent*/ if(($imagetype == 1) OR ($imagetype==3)) { imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $thumbwidth, $thumbheight, $transparent); }

imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumbwidth, $thumbheight, $widthorig, $heightorig);

//Generate the file, and rename it to $newfilename switch ($imagetype) { case 1: imagegif($newImg,$newfilename); break; case 2: imagejpeg($newImg,$newfilename);  break; case 3: imagepng($newImg,$newfilename); break; default:  triggererror(‘Failed resize image!’, EUSERWARNING);  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

do{ if($FILES[“img$imgNumb”]tmpname]!=”) { $success++; $Unique=microtime(); // We want unique names, right? $destination=$DestinationDir.md5($Unique).“.jpg”; $thumb=$ThumbDir.md5($Unique).“.jpg”;

$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]);

if($success>=1) { $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.

About me

Hello! I'm David Heward, how are you going? I'm a Senior Devops/Build Engineer, specialising in AWS & Cloud Automation. Based in London. Strong 10+ year background in Software development. Have a read of my blog. Have a look at some of my working projects. Contact me at @davehewy or on Linkedin.