Hi everyone, I am new to PHP and need help in making my script be able upload multiple images simultaneously. I need a large image and a thumb for each. I got this script from the web and made a few changes. I would really appreciate any help. The forms are much larger but i will try to get to the point and give what's necessary. Thanks in advance.

The following works for only one image:

The Database fields to insert to:

Establishment_mainphoto
Establishment_mainphoto_thumb

The form:

<form id="establishment_edit" name="establishment_edit" method="post" enctype="multipart/form-data" action="establishment_submit.php">

<input name="establishment_image" id="establishment_image" type="file" size="28"/><br>

<input type="submit" name="Submit" value="Submit"/>

</form>

This is the establishment_submit form(a little lengthy):

<?php require_once('../Connections/connections.php'); ?>
<?php //maintain the session
if (!isset($_SESSION))
{
  session_start();
}
?>
<?php 
//retrieve data from Query String		
$establishment_image = $_FILES['establishment_image']['name']; 

//escape User Input to help prevent SQL Injection
$establishment_image= mysql_real_escape_string($establishment_image);

//redirect when successful
$establishmentAddSuccess = "establishment_add_success.php";
?>
<?php
//define a maxim size for the uploaded images
define ("MAX_SIZE","1000");
// note that these dimmensions are considered the maximum dimmension and are not fixed
// because we have to keep the image ratio intact or it will be deformed
//define a maximum size for the uploaded images
define ("LARGE_WIDTH","500");
define ("LARGE_HEIGHT","390");
define ("WIDTH","100"); //set here the width you want your thumbnail to be
define ("HEIGHT","100"); //set here the height you want your thumbnail to be.
// this is the function that will create the appropriately sized images from the upload 
// the resize will be done considering the width and height defined, but without deforming the image

function make_largeimage($img_name,$filename,$new_large_w,$new_large_h)
{
	//get image extension.
	$ext=getExtension($img_name);
	//creates the new image using the appropriate function from gd library
	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
	$src_img=imagecreatefromjpeg($img_name);
	if(!strcmp("png",$ext))
	$src_img=imagecreatefrompng($img_name);
	if(!strcmp("gif",$ext))
	$src_img=imagecreatefromgif($img_name);
	//gets the dimmensions of the image
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	// next we will calculate the new dimmensions for the large image
	// the next steps will be taken:
	// 1. calculate the ratio by dividing the old dimmensions with the new ones
	// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
	// and the height will be calculated so the image ratio will not change
	// 3. otherwise we will use the height ratio for the image
	// as a result, only one of the dimmensions will be from the fixed ones
	$ratio1_large=$old_x/$new_large_w;
	$ratio2_large=$old_y/$new_large_h;
	if($ratio1_large>$ratio2_large) 
	{
		$large_w=$new_large_w;
		$large_h=$old_y/$ratio1_large;
	}else
	{
		$large_h=$new_large_h;
		$large_w=$old_x/$ratio2_large;
	}
	// we create a new image with the new dimmensions
	$dst_large_img=ImageCreateTrueColor($large_w,$large_h);
	// resize the big image to the newly created one
	imagecopyresampled($dst_large_img,$src_img,0,0,0,0,$large_w,$large_h,$old_x,$old_y);
	// output the created image to the file. Now we will have the image into the file named by $filename
	if(!strcmp("png",$ext))
		imagepng($dst_large_img,$filename);
	else
		imagejpeg($dst_large_img,$filename);
	if (!strcmp("gif",$ext))
	imagegif($$dst_large_img,$filename);
	//destroys source and destination images.
	imagedestroy($dst_large_img);
	imagedestroy($src_img);
}

function make_thumb($img_name,$filename,$new_w,$new_h)
{
	//get image extension.
	$ext=getExtension($img_name);
	//creates the new image using the appropriate function from gd library
	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
	$src_img=imagecreatefromjpeg($img_name);
	if(!strcmp("png",$ext))
	$src_img=imagecreatefrompng($img_name);
	if(!strcmp("gif",$ext))
	$src_img=imagecreatefromgif($img_name);
	//gets the dimmensions of the image
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	// next we will calculate the new dimmensions for the thumbnail image
	// the next steps will be taken:
	// 1. calculate the ratio by dividing the old dimmensions with the new ones
	// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
	// and the height will be calculated so the image ratio will not change
	// 3. otherwise we will use the height ratio for the image
	// as a result, only one of the dimmensions will be from the fixed ones
	$ratio1=$old_x/$new_w;
	$ratio2=$old_y/$new_h;
	if($ratio1>$ratio2) 
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y/$ratio1;
	}else
	{
		$thumb_h=$new_h;
		$thumb_w=$old_x/$ratio2;
	}
	// we create a new image with the new dimmensions
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	// resize the big image to the newly created one
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	// output the created image to the file. Now we will have the thumbnail into the file named by $filename
	if(!strcmp("png",$ext))
		imagepng($dst_img,$filename);
	else
		imagejpeg($dst_img,$filename);
	if (!strcmp("gif",$ext))
	imagegif($dst_img,$filename);
	//destroys source and destination images.
	imagedestroy($dst_img);
	imagedestroy($src_img);
}
// This function reads the extension of the file.
// It is used to determine if the file is an image by checking the extension.
function getExtension($str) 
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
// if it is not empty
if ($establishment_image)
{
// get the original name of the file from the clients machine
$filename = stripslashes($_FILES['establishment_image']['name']);

// get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
// if it is not a known extension, we will suppose it is an error, print an error message
//and will not upload the file, otherwise we continue
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$warning = ("File extension of image not allowed");
header("location:establishment_add.php?warning=$warning");	
$errors=1;
exit();
}
else
{
// get the size of the image in bytes
// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=getimagesize($_FILES['establishment_image']['tmp_name']);
$sizekb=filesize($_FILES['establishment_image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($sizekb > MAX_SIZE*1024)
{
$warning = ("Image has exceeded the size limit of 1MB");
header("location:establishment_add.php?warning=$warning");	
$errors=1;
exit();
}
$rand= rand(0, 100000);
//we will give an unique name, for example a random number
$image_name=$rand.'.'.$extension;
//the new name will be containing the full path where the image will be stored (images folder)
$consname="C:/wamp/www/NNL/Administrator/Establishment_Images/".$image_name; //change the image/ section to where you would like the original image to be stored
$consname2="C:/wamp/www/NNL/Administrator/Establishment_Images/Thumbs/".$image_name;
//change the image/thumb to where you would like to store the new created thumbnail of the image
$copied = copy($_FILES['establishment_image']['tmp_name'], $consname);
$copied = copy($_FILES['establishment_image']['tmp_name'], $consname2);
//localhost calling of images
$img_large="../Establishment_Images/".$image_name; //change the image/ section to where you would like the original image to be stored
$img_thumb="../Establishment_Images/Thumbs/".$image_name;
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
$warning = ("Unable to upload image file");
header("location:establishment_add.php?warning=$warning");	
$errors=1;
exit();
}else{
// the new large image will be placed in Images/ folder
$imagelarge_name=$consname ;
// call the function that will create the thumbnail. The function will get as parameters
// the image name, the thumbnail name and the width and height desired for the thumbnail
$imagelarge=make_largeimage($consname,$imagelarge_name,LARGE_WIDTH,LARGE_HEIGHT);
// the new thumbnail image will be placed in Images/Thumbs/ folder
$thumb_name=$consname2 ;
// call the function that will create the thumbnail. The function will get as parameters
// the image name, the thumbnail name and the width and height desired for the thumbnail
$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
}
}
}
?>
<?php
//If no errors registered, redirect page
if(isset($_POST['Submit']) && !$errors) 
{
//insert into database
$query2 = "INSERT INTO 							establishment_mainphoto,
establishment_mainphoto_thumb) 
VALUES 	
('$img_large',							'$img_thumb')";							
//Execute query
$qry_result2 = mysql_query($query2) or die(mysql_error());

header("Location: " . $establishmentAddSuccess);
}
else
{
	$establishment_msg = ("Unable to add establishment");
	header("location:establishment_add.php?establishment_msg=$establishment_msg");	
	exit();	
}		
?>

How do I change establishment_submit so that I can add images into a Database:

Establishment_mainphoto
Establishment_mainphoto_thumb
Establishment_photo1
Establishment_photo1_thumb
Establishment_photo2
Establishment_photo2_thumb
Establishment_photo3
Establishment_photo3_thumb
Establishment_photo4
Establishment_photo4_thumb
Establishment_photo5
Establishment_photo5_thumb

and a form like this?

<form id="establishment_edit" name="establishment_edit" method="post" enctype="multipart/form-data" action="establishment_submit.php">

<input name="establishment_image[]" type="file" size="28"/><br>
<input name="establishment_image[]" type="file" size="28"/><br>
<input name="establishment_image[]" type="file" size="28"/><br>
<input name="establishment_image[]" type="file" size="28"/><br>
<input name="establishment_image[]" type="file" size="28"/><br>
<input name="establishment_image[]" type="file" size="28"/><br>
<input type="submit" name="Submit" value="Submit"/>
</form>

Recommended Answers

All 6 Replies

you can get array of form field like this:

$uimages = $_POST['establishment_image'];
foreach( $uimages as $v ) 
{
 // single image
 print $v;
}

Thanks Shanti for your reply. But how do I resize the array of images so that i can store the 6 large and 6 thumbs in a server/folder and their locations in a database. I'm not very experienced in PHP.

how do I resize the array of images so that i can store the 6 large and 6 thumbs in a server/folder and their locations in a database. I'm not very experienced in PHP.

I think this is regardless of array..
Try with your existing code. But name your images with different names (concatinate image name with time and large/small) and save the path in database.

All the best.

Still trying to make it work. Thanks anyway

kkjay,

use these functions will help you:

//function for image uploading
function image_upload($image_name,$img_size,$img_temp,$path)
{
if($img_size>1) //for uploading image
{

$proimg=substr($image_name,0,strpos($image_name,'.'));
$commonname=time();
$name=substr($image_name,0,strpos($image_name,'.'));
$fullname=$name.$commonname.strstr($image_name,'.');
$real_image=$path.$fullname;
if(!move_uploaded_file($img_temp,$real_image))
{
$real_image="";
}
chmod($real_image,0777);
}
return $real_image;
}

function image_resize($i_old,$i_path,$size1,$size2)
{
$image_attribs = getimagesize($i_old);
if(substr($i_old,-3)=='gif') {
$im_old =imagecreatefromgif($i_old);
} else if(substr($i_old,-3)=='png') {
$im_old =imagecreatefrompng($i_old);
} else {
$im_old =imagecreatefromjpeg($i_old);
}
$width=$image_attribs[0];
$height=$image_attribs[1];
//
$th_max_width4= $size1;
$th_max_height4=$size2;
$ratio4 = ($width > $height) ? $th_max_width4/$image_attribs[0] : $th_max_height4/$image_attribs[1];
$th_width4 = $image_attribs[0] * $ratio4;
$th_height4 = $image_attribs[1] * $ratio4;
$im_new4 = imagecreatetruecolor($th_width4,$th_height4);
imageantialias($im_new4,true);
$th_file_name4= $i_path;
imagecopyresampled($im_new4,$im_old,0,0,0,0,$th_width4,$th_height4, $image_attribs[0], $image_attribs[1]);
if(substr($i_old,-3)=='gif') {
imagegif($im_new4,$th_file_name4,60);
} else if(substr($i_old,-3)=='bmp') {
imagewbmp($im_new4,$th_file_name4,60);
} else if(substr($i_old,-3)=='png') {
imagepng($im_new4,$th_file_name4,60);
} else {
imagejpeg($im_new4,$th_file_name4,60);
}
return $th_file_name4;
//
}

just call these two functions in your for loop by passing required parameters.
call same image_resize() function for large and small images with different size parameters..
i have tested this code. working fine for me.

thanks.

Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.