Jump to content


Photo

Multiple Image Upload


  • Please log in to reply
10 replies to this topic

#1 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 03 October 2006 - 05:23 PM

Hello.

I currently have a file upload with this code (Note: i've just taken out the screenshot part from the code):
<tr>
<td width="100">Screenshot</td> 
<td><input type="file" name="screenshot"><br></td>
</tr>

...

<?php
//This is the directory where images will be saved
$target = "../images/screenshots/";
$target = $target . basename( $_FILES['screenshot']['name']); 
$targettodb = str_replace("../","",$target);

$sql = "INSERT INTO site VALUES ('$targettodb')";
mysql_query($sql);

//Writes the screenshot to the server
if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded.";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}

This code works fine and uploads the pic and adds the info to the database, although i would like to make it so i can add more images. An example of what i am looking for is; like if you click additional options when replying and you have the attach bit, and you can choose to have 'more attachments' but then i need to add them them to the field in the database?

Any ideas/advice/code is appriciated.

Thanks.

Peter.

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 03 October 2006 - 05:35 PM

you can loop through the files array just like any other array...
do {
if(move_uploaded_file($_FILES[key($_FILES)]['tmp_name'], $target)) ..........
}while(next($_FILES));


#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 03 October 2006 - 06:23 PM

Name your fields like this:
<input type='file' name='the_name[]' />
<input type='file' name='the_name[]' />
and then do something like what michaellunsford showed you.

#4 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 03 October 2006 - 06:36 PM

Hello.

Well after doing a bit of 'googling' i found this:
http://the-stickman....e-file-element/

This is great and very similar to what i wanted, so i added it into my code:

-- Edit -- Ok after writing it all it seems like it's not going to let me post to code as every time i click post it just asks me to download index.php, so i'll try editing it in.

--Edit #2 -- Ok, well it won't even let me attach without messing up, so the file with the code in is here:
http://www.filefacto...om/file/88a507/

Although the images upload fine (even though it does say 'Sorry, there was a problem uploading your file.') it still uploaded all the files BUT nothing was added to the database, I assume this is something to do with what michaellunsford has said, although i am reasonably new to php and don't quite understand how i should change this bit to work with the script i put on.

Has anyone got any advice?

Thanks for all your help!

Peter.

#5 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 03 October 2006 - 06:41 PM

the loop I gave you could effectively surround your entire PHP code (that you provided above). Be sure to use key($_FILES) in place of $_FILES['uploadedfile'] or $_FILES['screenshot'] -- which seem to be used interchangeably in your example.

#6 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 03 October 2006 - 06:53 PM

I've updated the post above, does the same still apply?

Thanks.

Peter.

#7 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 03 October 2006 - 07:00 PM

It should still apply. Be sure to include the database updates inside the loop.

#8 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 03 October 2006 - 07:54 PM

you can loop through the files array just like any other array...

do {
if(move_uploaded_file($_FILES[key($_FILES)]['tmp_name'], $target)) ..........
}while(next($_FILES));


Does this code just upload the files to the server? Although even with the new code i added which i talked about a few posts ago the images are still uploaded alright it's just not adding the values to the database. Can someone help explain where i'm going wrong, i'm still not that good at PHP :P?

Thanks.

Peter.

#9 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 03 October 2006 - 08:03 PM

the code inside the do { }while loop is just an example. It doesn't actually do anything. You would need to apply your own code.

#10 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 03 October 2006 - 08:05 PM

Hi,

You will need to edit this to suit, this is wat I use along with gd code:

$number_of_fields = 4;

	echo '<form enctype="multipart/form-data" action="index.php?action=upload" method="post" name="upload_form">';

  while($counter <= $number_of_fields){
  	echo '<input name="photo_filename[]" type="file" class="input-box"><br />';
	echo '<textarea name="photo_caption[]" cols="26" rows="3" class="input-box"></textarea><br /><br />';
  $counter++;
}

	echo '<input type="submit" name="submit" value="Upload Photos" class="submit-button">';
	echo '</form>';

	echo '</center>';

and to process:

$counter = 0;	

$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif');
        
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'gif');

	$photos_filename = $_FILES['photo_filename'];        
	$photo_caption = $_POST['photo_caption'];

	while($counter <= count($photos_uploaded))
{
	if($photos_uploaded['size'][$counter] > 0)
{
	if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
{
	echo 'File '.($counter+1).' is not a photo!<br />';

}else{

	mysql_query("INSERT INTO gallery_images(photo_filename, photo_caption) VALUES('".$photo_filename[$counter]."', '".$photo_caption[$counter]."')" );
copy($photos_filename['tmp_name'][$counter], $images_dir."/".$filename);
}

hth

#11 JJohnsenDK

JJohnsenDK
  • Members
  • PipPipPip
  • Advanced Member
  • 400 posts

Posted 21 December 2006 - 10:20 AM

I have a quesstion about the last posted code in this thread.

I dont understand this line of code:

while($counter <= count($photos_uploaded))

Where does $photos_uploaded come form? $counter cant be <= to a number there does not exits??




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users