wwfc_barmy_army Posted October 3, 2006 Share Posted October 3, 2006 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 serverif(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)){//Tells you if its all okecho "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded.";}else {//Gives and error if its notecho "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. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/ Share on other sites More sharing options...
michaellunsford Posted October 3, 2006 Share Posted October 3, 2006 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 Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103213 Share on other sites More sharing options...
Daniel0 Posted October 3, 2006 Share Posted October 3, 2006 Name your fields like this: [code]<input type='file' name='the_name[]' /><input type='file' name='the_name[]' />[/code] and then do something like what michaellunsford showed you. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103246 Share on other sites More sharing options...
wwfc_barmy_army Posted October 3, 2006 Author Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103258 Share on other sites More sharing options...
michaellunsford Posted October 3, 2006 Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103266 Share on other sites More sharing options...
wwfc_barmy_army Posted October 3, 2006 Author Share Posted October 3, 2006 I've updated the post above, does the same still apply?Thanks.Peter. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103280 Share on other sites More sharing options...
michaellunsford Posted October 3, 2006 Share Posted October 3, 2006 It should still apply. Be sure to include the database updates inside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103289 Share on other sites More sharing options...
wwfc_barmy_army Posted October 3, 2006 Author Share Posted October 3, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103326 Share on other sites More sharing options...
michaellunsford Posted October 3, 2006 Share Posted October 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103336 Share on other sites More sharing options...
bob_the _builder Posted October 3, 2006 Share Posted October 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-103339 Share on other sites More sharing options...
JJohnsenDK Posted December 21, 2006 Share Posted December 21, 2006 I have a quesstion about the last posted code in this thread.I dont understand this line of code:[i]while($counter <= count($photos_uploaded))[/i]Where does $photos_uploaded come form? $counter cant be <= to a number there does not exits?? Quote Link to comment https://forums.phpfreaks.com/topic/22894-multiple-image-upload/#findComment-145750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.