Multiple Image Upload Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itcassy
Forum Newbie
Posts: 1
Joined: Mon Oct 16, 2006 3:32 pm

Multiple Image Upload Problem

Post by itcassy »

pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I know there are several image tutorials, however I am having an issue with multiple image upload that I cannot see an answer to. I would GREATLY appreciate any assistance.

I have a page where the user inputs info that gets inserted into the db.  Also, on the same page, the user can upload up to three images.  Everything works great (all info is inserted, images go to correct folder, correct image name goes into db) HOWEVER, you HAVE to upload all 3 images for it to work properly. All 3 fields may not all have images all of the time. I have the images using the time in the filename when uploading so the names are unique and if you do not have an image in the upload field, just the time stamp and image directory name is inserted into the db as the file name. I have tried using if statements to verify that there is a file in the upload field but have failed miserably with all attempts. I am at a novice to intermediate level in my coding. I did find some of this code online. Below is the code:

[syntax="php"]
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the directory that will recieve the uploaded file
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'files/';

// make a note of the location of the upload form in case we need it
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'pubs_view.php?action=add';

// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload_success.php';

// fieldname used within the file <input> of the HTML form
$cover = 'cover';
$pubtitle = 'pubtitle';
$sample = 'sample';

// Deal with upload
// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
		2 => 'html form max file size exceeded',
		3 => 'file upload was only partial',
		4 => 'no file was attached');
							
// check the upload form was actually submitted else print the form
	isset($_POST['submit'])
    	or error('the upload form is neaded', $uploadForm);

// check that the file we are working on really was the subject of an HTTP upload - I commented this out for troubleshooting
//	@is_uploaded_file($_FILES[$cover]['tmp_name'])
//   	or error('not an HTTP upload', $uploadForm);
    
// make sure it's an image - I commented this out for troubleshooting
// getimagesize() returns false if the file tested is not an image.
//	@getimagesize($_FILES[$cover]['tmp_name'])
//   	or error('only image uploads are allowed', $uploadForm);
			
    
// make a unique filename... if it is already taken keep trying until we find a vacant one
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$cover]['name']))
	{
    	$now++;
	}
			
while(file_exists($uploadFilename2 = $uploadsDirectory.$now.'-'.$_FILES[$pubtitle]['name']))
	{
    	$now++;
	}
			
while(file_exists($uploadFilename3 = $uploadsDirectory.$now.'-'.$_FILES[$sample]['name']))
	{
    	$now++;
	}

//move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$cover]['tmp_name'], $uploadFilename)
	or error('receiving directory insuffiecient permission', $uploadForm);
				
@move_uploaded_file($_FILES[$pubtitle]['tmp_name'], $uploadFilename2)
	or error('receiving directory insuffiecient permission', $uploadForm);
				
@move_uploaded_file($_FILES[$sample]['tmp_name'], $uploadFilename3)
	or error('receiving directory insuffiecient permission', $uploadForm);
				
//create filename for db
$dbfilename = $now.'-'.$_FILES[$cover]['name'];
$dbfilename2 = $now.'-'.$_FILES[$pubtitle]['name'];
$dbfilename3 = $now.'-'.$_FILES[$sample]['name'];
			
//declare values for add
$SQL = "UPDATE publications SET ";
$SQL .= "title = '" . $_POST['title'] . "', ";
$SQL .= "compiler = '" . $_POST['compiler'] . "', ";
$SQL .= "description = '" . $_POST['description'] . "', ";
$SQL .= "nprice = '" . $_POST['nprice'] . "', ";
$SQL .= "mprice = '" . $_POST['mprice'] . "', ";
$SQL .= "intro = '" . $_POST['intro'] . "', ";
$SQL .= "cover = '" . $dbfilename . "', ";
$SQL .= "pubtitle = '" . $dbfilename2 . "', ";
$SQL .= "sample = '" . $dbfilename3 . "' ";
$SQL .= "WHERE ID = '" . $_POST['ID'] . "'";			

If there is someplace where this is listed, I apologize in advance and would appreciate a link. Thanks!


pickle | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It would seem that a function could be written to handle each file individually returning the database entry value to use.

Your while loop set should be checking all the filenames during each pass, by the way. Also, some browsers supply a full path for the "name" element, you may want to use basename().
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could put your conditions & file moving code in a loop:

Code: Select all

foreach($_FILES as $particular_file)
{
   do
  {
    $db_name = uniqid().'-'.$particular_file['name'];
    $unique_name = $uploadsDirectory.$db_name;
  }
  while(!file_exists($unique_name));

  move_uploaded_file($particular_file['tmp_name'],$unique_name) or error('receiving directory insufficient permission',$uploadForm);

  //then on to inserting into the db
}


Also, your usage of time() to ensure a unique name is not 100% fool-proof. Incrementing time by 1 will just add a second. So, if someone uploads 3 identical file names, the names will span 3 seconds - during which someone else could also upload an identically named image. Try using uniqid() instead.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply