This is an updated/edited post of a script which works great for uploading multiple images. The problem is when trying to create a thumb for each image uploaded. GALLERY_IMG_PATH, GALLERY_IMG_FULLSIZE, and GALLERY_IMG_THUMB are defined in the config file those are just path information. The script I'm using:
$errors = array();
$uploadedFiles = array();
$extension = array("jpeg","jpg","png");
$bytes = 1024;
$KB = 1024;
$totalBytes = $bytes * $KB;
$counter = 0;
if(isset($_FILES["images"]["tmp_name"])) {
foreach($_FILES["images"]["tmp_name"] as $key=>$tmp_name) {
$temp_Name = $_FILES["images"]["tmp_name"][$key];
$images_Name = $_FILES["images"]["name"][$key];
if(empty($temp_Name)) {
break;
}
$counter++;
$UploadOk = true;
if($_FILES["images"]["size"][$key] > $totalBytes) {
$UploadOk = false;
array_push($errors, $images_Name." file size is larger than the 1 MB. You must
upload images smaller than 1MB.");
}
$ext = strtolower(pathinfo($images_Name, PATHINFO_EXTENSION));
if(in_array($ext, $extension) == false){
$UploadOk = false;
array_push($errors, $images_Name." is an invalid file type. Images must be
either of the following: jpeg | jpg | png");
}
if(file_exists(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name) ==
true) {
$UploadOk = false;
array_push($errors, "Attempted to upload an image that is already uploaded/the
image name conflicts with an image that already uses the
name:".$images_Name.".");
}
if($UploadOk == true) {
move_uploaded_file($temp_Name,
GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name);
array_push($uploadedFiles, $images_Name);
// Get the image size and type & Load the image into memory
$attrs = getimagesize (GALLERY_IMG_PATH."/".$images_Name);
$imageWidth = $attrs[0];
$imageHeight = $attrs[1];
$imageType = $attrs[2];
switch ($imageType) {
case IMAGETYPE_JPEG:
$imageResource = imagecreatefromjpeg
(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name);
break;
case IMAGETYPE_PNG:
$imageResource = imagecreatefrompng
(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name);
break;
default:
trigger_error ("Post::storeUploadedImage(): Unhandled or unknown image type
($imageType)", E_USER_ERROR);
}
// Copy and resize the image to create the thumbnail
$thumbHeight = intval ($imageHeight / $imageWidth * GALLERY_THUMB_WIDTH);
$thumbResource = imagecreatetruecolor (GALLERY_THUMB_WIDTH, $thumbHeight);
imagecopyresampled($thumbResource, $imageResource, 0, 0, 0, 0,
GALLERY_THUMB_WIDTH, $thumbHeight, $imageWidth, $imageHeight);
// Save the thumbnail
switch ($imageType) {
case IMAGETYPE_JPEG:
imagejpeg ($thumbResource, GALLERY_IMG_PATH."/".GALLERY_IMG_THUMB, 100);
break;
case IMAGETYPE_PNG:
imagepng ($thumbResource, GALLERY_IMG_PATH."/".GALLERY_IMG_THUMB, 100);
break;
default:
trigger_error ("Post::storeUploadedImage(): Unhandled or unknown image type
($imageType)", E_USER_ERROR);
}
}
}
}
if($counter>0){
if(count($errors)>0)
{
echo "<b>Errors:</b>";
echo "<br/><ul>";
foreach($errors as $error) {
echo "<li>".$error."</li>";
}
echo "</ul><br/>";
}
if(count($uploadedFiles)>0) {
echo "<b>Uploaded Files:</b>";
echo "<br/><ul>";
foreach($uploadedFiles as $fileName) {
echo "<li>".$fileName."</li>";
}
echo "</ul><br/>";
echo count($uploadedFiles)." file(s) have successfully uploaded.";
}
}
The html form is:
<form action="editGallery" method="post" enctype="multipart/form-data" name="formUploadFile">
<label for="files[]">Select image/images to upload:</label>
<input type="file" name="images[]" id="images" accept="image/*" multiple="multiple"
placeholder="Upload an image/images.">
<input type="submit" name="uploadImages" value="UploadImages">
</form>
Right now the script is executing and errors out after one image uploads to the fullsize directory and from what I'm guessing is it attempts to create the thumb and gives an error.