help with uploading images for a gallery

I am working on a website that I inherited at work and am still learning php so please bear with me.
The following code is used to upload photos for an image gallery, but I am wondering if there is a way to change this to allow me to upload multiple files at once, rather than one at a time. (There are two tables in the MySQL database … one called galleryAlbum and one called gallery)

[php]

$uploaddir = $modulesDir.$moduleDir.“galleryimages/”; // Directory to store the main images
$uploadTHdir = $modulesDir.$moduleDir.“gallerythumbs/”; // Directory to store the thumbnails

$curpage = $_GET[‘curpage’];

echo “<span class=“adminHeader”>Image Gallery Administration

”;

IF ($do == “addImage”) {

echo “<span class=“actionHead”>Add an Image

”;

$page=$_GET[‘page’];

$descriptionPost = addslashes($_POST[‘description’]);
$albumPost = addslashes($_POST[‘album’]);
$image = $_FILES[‘theImage’][‘name’];

if($page==“submit”){$error = chkGalleryElements($image,$albumPost,$descriptionPost);}

IF (($page=="submit")&&($error=="5")){

$maxfilesize = 5000000;

$query = "SELECT MAX(iid) FROM gallery";
$result=mysql_query($query, $connection) or die("Query failed : " . mysql_error());
$maxImageId = max(mysql_fetch_array($result)); 
IF ($maxImageId==null){
$maxImageId=0;
}

$imageName = $maxImageId + 1;

$uploadfile = "$uploaddir$imageName.jpg";
$uploadTHfile = $uploadTHdir."th_".$imageName.".jpg";
$uploadsize = $_FILES['theImage']['size'];
					
/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/

$pext = getFileExtension($image);
$pext = strtolower($pext);
if (($pext != "jpg")  && ($pext != "jpeg")) {
	print "<h1>ERROR</h1> The file you uploaded had the following extension: $pext.<br>";
	print "<p>Please upload an image with the extension .jpg or .jpeg ONLY.<br><br>";
	
	echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
	
} elseif ($maxfilesize <= $uploadsize){
	echo "The image that you have uploaded is too big.  The system will accomodate any images up to 5MB and resize them to ensure optimal browsing.  If your file is larger than this, please resize the image.";
	echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
} else {
	if (move_uploaded_file($_FILES['theImage']['tmp_name'], $uploadfile)) {
		
		// The file
		$filename = $uploadfile;
		$desiredwidth = 390;
		$desiredthumb_width = 150;
		
		// Get new dimensions
		list($width, $height) = getimagesize($filename);
		IF ($width > 390){
		$new_width = $desiredwidth;
		$new_height = $desiredwidth/$width * $height;
		}
		ELSE{
		$new_width = $width;
		$new_height = $height;
		}
		
		IF ($width > 150){
		$new_thumb_width = $desiredthumb_width;
		$new_thumb_height = $desiredthumb_width/$width * $height;
		}
		ELSE{
		$new_thumb_width = $width;
		$new_thumb_height = $height;
		}
		
		// Resample
		$image_p = imagecreatetruecolor($new_width, $new_height);
		$image = imagecreatefromjpeg($filename);
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		
		$image_t = imagecreatetruecolor($new_thumb_width, $new_thumb_height);
		$imagethumb = imagecreatefromjpeg($filename);
		imagecopyresampled($image_t, $imagethumb, 0, 0, 0, 0, $new_thumb_width, $new_thumb_height, $width, $height);
		
		// Output
		imagejpeg($image_p, "$uploadfile", 100);
		imagejpeg($image_t, "$uploadTHfile", 100);
		
		$query = "INSERT INTO gallery VALUES ('','$imageName','$descriptionPost','$albumPost')";
		mysql_query($query, $connection) or die("Query failed : " . mysql_error());
					
		echo "Thank-you image has been added to the gallery.<br /><br />";
		echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
		} 
		else {
			echo "File upload is not valid.  Check permissions or contact the system administrator.";
			echo "<br /><br />";
			echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
		}
	}
} 	

ELSE{
	//code to display the editor which replaces the textarea.
	  echo "<script type=\"text/javascript\" src=\""
      ."$docroot"
    ."editor/fckeditor.js\"></script>"
	."<script type=\"text/javascript\">"
	."window.onload = function()"
	."{"
	."var oFCKeditor = new FCKeditor( 'description','','250','Default','' ) ;"
	."oFCKeditor.BasePath	= '"
     ."$docroot"
    ."editor/' ;"
	."oFCKeditor.ReplaceTextarea() ;"
	."}"
	."</script>";
	//editor code end

	IF ($error==2){
	echo "<font color=\"red\">Please check to ensure that all fields have been filled.</font><br /><br />";
	$error==0;
	}
	
echo "<table width=\"100%\">"
     ."<form enctype=\"multipart/form-data\" action=\"default.php?$urlString&page=submit\"  method=\"POST\">"
				
		."<tr><td width=\"120\">"
		//<!-- Name of input element determines name in $_FILES array -->
		."<b>Choose Image: </b></td><td><input name=\"theImage\" type=\"file\" /></td></tr>"
		."<tr><td width=\"80\"><strong>Add to Album:</strong></td><td>"
		."<select name=\"album\" size=\"1\">"
		."<option selected";
	     	if (($albumPost!=null)||($albumPost!="")){
			echo "value=\"$albumPost\">";
			$albumNameQuery = "SELECT * from galleryalbum where aid=$albumPost";
			$albumNameResult = mysql_query($albumNameQuery, $connection) or die("Query failed : " . mysql_error());
			$albumName = mysql_result($albumNameResult,0,"aname");	
			echo stripslashes($albumName);
		}
		else{
			echo ">";
		}
		 echo "</option>";
		
			$query = "SELECT * FROM galleryalbum";
			$result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());
			$numrows = mysql_num_rows($result);

			for ($i = 0; $i < $numrows; $i++) {			
				$aname = mysql_result($result,$i,"aname");
				$aid = mysql_result($result,$i,"aid");
				
				echo "<option value=\"$aid\">";
				echo $aname;
				echo "</option>";
			}
			echo "</select>";
		echo "</td></tr>"
		
		."<tr><td colspan=\"2\" height=\"10\"></td></tr>"	
		."<tr><td colspan=\"2\"><b>Description:</b><br /></td></tr>"	
		."<tr><td colspan=\"2\" height=\"100\"><textarea name=\"description\" rows=\"8\" cols=\"75\">";
		if (($descriptionPost!=null)||($descriptionPost!="")){
			echo stripslashes($descriptionPost);
		}
		echo "</textarea></td></tr>"
		."<tr><td colspan=\"2\" height=\"10\"></td></tr>"	
					
		."<tr><td colspan=\"2\" height=\"10\">"
		."<input type=\"submit\" value=\"Submit\" />"
		."</td></tr>"
		."</form>"
		."</table>";
		
		echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
}

}[/php]

Yes, it can be done, but, you need to create a very complicated front-end to keep tract of all the HTTP requests that you would be sending to the PHP file. Remember, FILES are CLIENT-SIDE and PHP is SERVER-SIDE. PHP being SERVER-SIDE can NOT see the user’s computer and can NOT upload files from there.

Files are sent using HTTP or FTP to the server and once there, the PHP takes over. So, multiple files are very tricky.

BUT, there is a really nice solution that I just tested for a project of my own. It works great. It is called Uploadify. It is basically a trick to send multiple files across HTTP. But, it uses a flash plugin that sends the multiple files. There are several issues which I had to sort out. First, the server must be set up to allow multiple files and it must be set up for higher max of file sizes.

To explain, if you have a standard high quality picture, it may be 1 meg in size. If your standard upload limit is 2 megs, which is the standard, then you can only do two pictures at a time. There are about five settings that have to do with uploads. When you get to that point, reply here for further help. It was fairly easy to add this plugin in and I think it will do fine for you…

The link to it is: http://www.uploadify.com/ The flash version is free and works. Since not everyone has flash, you might need to use a different plugin. I feel that most people have flash on their system, so I use it.

Another one that I really like, but, could not get to work is Pluploader. It uses several transport plugin’s to get the work done. It is fancier than Uploadify, but, much more complicated. It’s here: http://www.plupload.com/

So, quick recap on all that… Not possible to use standard HTTP to PHP for multiple files. Well, not easy. You have to send each file separately and track sessions as HTTP links to PHP using a session number. This session number does not allow for more than one file. So, you would have to create multiple HTTP sessions and that is so very tricky to do that it is not worth the time.

Well, I have spent three months trying about 40 programs and these are the best two in my opinion.
Good luck, let us know how it works out.

What! Uploading multiple files in php is easy, I do it all the time. You simply use a foreach loop on the $_FILES array and process as you normally would for a single file. You can also put the multiple=“multiple” attribute on the file input to allow multiple file selection from one input rather than having multiple inputs and only selecting one file in each.

To the OP, I didn’t go through your code fully to figure out how to incorporate this method in your specific code, but it is very possible.

Sorry, fastsol, you can NOT do mutlitple HTTP posts at a time.

You CAN loop thru a $_FILES array and do one at a time. That is not Multiple uploads, well, technically it is, but, it is not mutliple-all-at-once or simultaneously.

How HTTP handles multiple files is one large post. Standard server settings are set to 2megs per post. So, if you post one pix of 1meg, you have room for 1 more meg of data from a form or for another pix. PHP code is not started until the entire POST is uploaded to the server. Your question was

I am wondering if there is a way to change this to allow me to upload multiple files at once, rather than one at a time.
The short answer to that is no. This is due to HTTP uploads being one POST stream. The POST is actually two arrays, $_POST and $_FILES, as you seem to understand, but, it is one large stream of data. Your PHP code takes over after the POST is completed. PHP, unfortunately, is NOT a multithreading system. Therefore, it must handle the data one at a time using the two arrays at hand.

Sorry if I was not clear before. Multiple files in an array is not the same as multitasking.

Sponsor our Newsletter | Privacy Policy | Terms of Service