Page 1 of 1

Multiple image upload to different directories

Posted: Tue May 18, 2010 6:54 am
by agentdylan
Hi there,

I'm building a content management system for a web gallery where you are able to upload multiple images from the cms. So far I have a form which submits the Title, Description and image paths are entered manually in my CMS and the images are having to be uploaded through an ftp client, obviously this is not user friendly.

I need to be able to upload thumbnails, preview images and full sized images. I would like the thumbnails to go to images/gallery/thumb/ the preview images to go to images/gallery/preview/ and the full sized images to go to images/gallery/full/ so far I have been unable to find a solution. The uploaded files would then also need to have their name and path saved to the mysql database.

I have tried many different file uploads but so far all multiple image uploaders seem to upload to the same directory. I have also tried having a page to upload the images individually from new windows (as I am then able to change their individual path), however I am then unable to get the image name and path to enter into the form and submit to the mysql database.

Any and all help appreciated, thanks. :)

I was able to get this script to upload two images to different directories, however the images were saved with the same name.
This is the code I'm currently using to upload an image individually:

Code: Select all

<?php
 define ("MAX_SIZE","1000"); 
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if(isset($_POST['Submit'])) 
 {
 	 	$image=$_FILES['image']['name'];
 
 	if ($image) 
 	{

 		$filename = stripslashes($_FILES['image']['name']);

  		$extension = getExtension($filename);
 		$extension = strtolower($extension);

 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
	
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{

 $size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
	echo '<h1>You have exceeded the size limit!</h1>';
	$errors=1;
}

$image_name=$filename;

$newname="../../images/gallery/thumb/".$image_name;

$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
	echo '<h1>Copy unsuccessfull!</h1>';
	$errors=1;
}}}}

 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h1>File Uploaded Successfully!</h1>";
 }

 ?>

  <form name="newad" method="post" enctype="multipart/form-data"  action="">
 <table>
 	<tr><td><input type="file" name="image"></td></tr>
 	<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
 </table>	
 </form>