Jump to content

Multiple Image Upload


wwfc_barmy_army

Recommended Posts

Hello.

I currently have a file upload with this code (Note: i've just taken out the screenshot part from the code):
[code=php:0]<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.";
}
[/code]

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.
Link to comment
Share on other sites

Hello.

Well after doing a bit of 'googling' i found this:
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-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.filefactory.com/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.
Link to comment
Share on other sites

[quote author=michaellunsford link=topic=110393.msg446146#msg446146 date=1159896934]
you can loop through the files array just like any other array...
[code=php:0]
do {
if(move_uploaded_file($_FILES[key($_FILES)]['tmp_name'], $target)) ..........
}while(next($_FILES));
[/code]
[/quote]

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.
Link to comment
Share on other sites

Hi,

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

[code=php:0] $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>';[/code]

and to process:

[code=php:0]$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);
}[/code]

hth
Link to comment
Share on other sites

  • 2 months later...
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.