Jump to content

Recommended Posts

Hi everyone, great forum you have here! Anyway i hope someone may be able to help!!! The script below allows me upload 1 image and create a thumbnail as well as adding the file name and other info from my form to my database! Lovely it works a charm and allows me to do everthing i want. However, is there a way i can adapt the script to allow multiple uploads, i know about extending the fields to accept new file names ect, its just the upload and thumbnail part of the script to accept more than one image at time.

Thanks in advance for taking the time to read this, any help would be greatly appreciated.

 

$idir = "dbimages/";

$tdir = "dbimages/thimages/";

$twidth = "120";

$theight = "120";

$lat=$_POST['lat'];

$lng=$_POST['lng'];

$type=$_POST['type'];

$address=$_POST['address'];

$beds=$_POST['beds'];

$name=$_POST['name'];  

$pic=( $_FILES['photo']['name']);

mysql_connect("dbserver", "dbuser", "dbpass") or die(mysql_error()) ; 

mysql_select_db("dbname") or die(mysql_error()) ;



mysql_query("INSERT INTO `dbtable` (lat, lng, type, address, beds, name, photo) VALUES ('$lat', '$lng', '$type', '$address', '$beds', '$name', '$pic')") ;

  $url = $_FILES['photo']['name']; 

  if ($_FILES['photo']['type'] == "image/jpg" || $_FILES['photo']['type'] == "image/jpeg" || $_FILES['photo']['type'] == "image/pjpeg") { 

    $file_ext = strrchr($_FILES['photo']['name'], '.');   

    $copy = copy($_FILES['photo']['tmp_name'], "$idir" . $_FILES['photo']['name']);

    if ($copy) { 

      $simg = imagecreatefromjpeg("$idir" . $url);

      $currwidth = imagesx($simg);

      $currheight = imagesy($simg); 

      if ($currheight > $currwidth) {

         $zoom = $twidth / $currheight; 

         $newheight = $theight;

         $newwidth = $currwidth * $zoom;

      } else {

        $zoom = $twidth / $currwidth; 

        $newwidth = $twidth;

        $newheight = $currheight * $zoom;

      } 

      $dimg = imagecreate($newwidth, $newheight);

      imagetruecolortopalette($simg, false, 256); 

      $palsize = ImageColorsTotal($simg); 

      for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);

       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);

      } 

      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 

      imagejpeg($dimg, "$tdir" . $url);

      imagedestroy($simg);

      imagedestroy($dimg);

Link to comment
https://forums.phpfreaks.com/topic/86889-multiple-upload-and-resize/
Share on other sites

  • 2 months later...

that is a very dated upload script

 

I wrote this and its fairly simple for a 5 image upload (can be moded to anywhere from 1-1000000

no resize  just moves uploaded files to a tmp storage folder with a tmp name that is stored in a  mysql table "PICTURES_TABLE"

 

this is the php file it contains the form in it

<?php
<?php
#print_r($_POST);
#print_r($_FILES);
define("PICTURES_TABLE", "Pictures");
ConnectSQL();
if(!empty($_POST)){
if($_POST['no_refresh'] != $_SESSION['no_refresh']){
$i = 1;
while($i <6){
	if($_FILES['image_'.$i]['error'] == "0"){
		if($_FILES['image_'.$i]['size'] < $_POST['MAX_FILE_SIZE']){
			if(in_array($_FILES['image_'.$i]['type'],$image_types)){
				#Upload Location
				$rand_file= ROOT."uploads/";
				$file_loc = date("U")."_".rand(0,9999);
				switch($_FILES['image_'.$i]['type']){
					case "image/jpeg":
					case "image/pjpeg":
						$ext = ".jpg";
					break;
					break;
					case "image/gif":
					case "image/pgif":
						$ext = ".gif";
					break;
					break;
					case "image/png":
					case "image/ppng":
						$ext = ".png";
					break;
					break;
				}
				#file moving
				move_uploaded_file($_FILES['image_'.$i]['tmp_name'],$rand_file.$file_loc.$ext);
				#insert query
				$q = "Insert into `".PICTURES_TABLE."`
					(Gallery_ID, Upload_IP, Upload_Name, Upload_Email, Upload_Date, Tmp_Name, O_Name)
					VALUES('".input_clean($_POST['pic_type'])."', '".input_clean($_POST['ip'])."', '".$_POST['name']."', '".$_POST['email']."', NOW(), '".$file_loc.$ext."', '".$_FILES['image_'.$i]['name']."')";
				$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
				$no_errors[] = "The image was uploaded!";
				$_SESSION['no_refresh'] = $_POST['no_refresh'];
			}
			else{
				$errors[] = "The file type is invalid for the file uploaded.";
			}
		}
		else{
			$errors[] = "The file uploaded was too large.";
		}
	}
	else{
		if(!empty($_FILE['image_'.$i])){
			$errors[] = "There was an error uploaded the file.";
		}
	}
	$i++;
}
}
else{
	$errors[] = "Please do not refresh this page.";
}
}
echo "<h1>Photo Upload</h1>";
if(!empty($errors)){
echo "<br /<br />";
echo "<h3>Please fix the following errors: </h3>";
echo "<ul class=\"error_ul\">";
	foreach($errors as $value){
		echo "<li>".$value."</li>";
	}
echo "</ul>";
echo "<br /><hr /><br />";
}
if(!empty($no_errors)){
echo "<h3>Successs!</h3>";
echo "<ul class=\"noerror_ul\">";
	foreach($no_errors as $value){
		echo "<li>".$value."</li>";
	}
echo "</ul>";
echo "<br /><br />";
}
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" enctype=\"multipart/form-data\">\n";
echo "<input type=\"hidden\" value=\"".$_SERVER['REMOTE_ADDR']."\" />\n";
echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"5000000\">";
echo "<input type=\"hidden\" name=\"no_refresh\" value=\"".rand(0,9999)."\" />";
echo "Your Name (optional): <input type=\"text\" name=\"name\" value=\"\" /><br />\n";
echo "Your Email (optional): <input type=\"text\" name=\"email\" value=\"\" /><br />\n";
echo "Category/Event: \n";
echo "<select name=\"pic_type\">\n";
echo "<option value=\"\">None</option>\n";
#Add more options here for fields
echo "</select><br />\n";	
echo "Image 1: <input type=\"file\" name=\"image_1\" /><br />\n";
echo "Image 2: <input type=\"file\" name=\"image_2\" /><br />\n";
echo "Image 3: <input type=\"file\" name=\"image_3\" /><br />\n";
echo "Image 4: <input type=\"file\" name=\"image_4\" /><br />\n";
echo "Image 5: <input type=\"file\" name=\"image_5\" /><br />\n";
echo "<input type=\"submit\" value=\"Upload\" />";	
echo "</form>";
?>

 

And the mysql dump of pictures

CREATE TABLE `Pictures` (
  `PicID` bigint(20) NOT NULL auto_increment,
  `Gallery_ID` varchar(24) collate utf8_unicode_ci NOT NULL default '0',
  `Upload_IP` varchar(18) collate utf8_unicode_ci NOT NULL default '',
  `Upload_Name` varchar(128) collate utf8_unicode_ci NOT NULL default '',
  `Upload_Email` varchar(128) collate utf8_unicode_ci NOT NULL default '',
  `Upload_Date` datetime NOT NULL default '0000-00-00 00:00:00',
  `Tmp_Name` varchar(128) collate utf8_unicode_ci NOT NULL default '',
  `Approved` tinyint(1) NOT NULL default '0',
  `O_Name` varchar(128) collate utf8_unicode_ci NOT NULL default '',
  PRIMARY KEY  (`PicID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Using an array like demonstrated in the manual's example would allow the form processing code to be made generic using a foreach() loop so it would not need to carry around a number to tell it how many images and it would allow the form to dynamically add upload fields using javascript.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.