JJohnsenDK Posted December 19, 2006 Share Posted December 19, 2006 HeyIm wondering if its possible to upload/update several files with the same code or shall i copy/paste the code 5 times?Here is the code im using to upload a file.[code]<?php$update_img = mysql_query("UPDATE forside SET billede1 = '".$_FILES['img1']['name']."', billede2 = '".$_FILES['img2']['name']."', billede3 = '".$_FILES['img3']['name']."', billede4 = '".$_FILES['img4']['name']."', billede5 = '".$_FILES['img5']['name']."'") or die(mysql_error()); { $tempfile = $_FILES['img1']['tmp_name']; $destination = "../billeder/forside/{$_FILES['img1']['name']}"; move_uploaded_file($tempfile, $destination); }?>[/code]As you can see i have 5 tables and each of them should also upload a file to a folder with im using the above code for. But it only uploads the first file. Is it possible to rewrite that code so it uploads all the 5 files? or is there no ohter way than to copy/paste the code 5 times? Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/ Share on other sites More sharing options...
HuggieBear Posted December 19, 2006 Share Posted December 19, 2006 You need to use a foreach loop really. Never copy and paste the same code. If the same code's being used more than once, then surely there's cause for a function there?You obviously have a form that contains five fields called img1, img2, img3, img4, and img5. First thing you need to do is change the names of these. You want to submit an array rather than individual names, so rename them something like images[] (make sure whatever you call the field, you have the square brackets after it e.g.[code]<input type="file" name="images[]"><input type="file" name="images[]"><input type="file" name="images[]"><input type="file" name="images[]"><input type="file" name="images[]">[/code]I'll post some sample code a little later so you can see something similar that I have.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/#findComment-144334 Share on other sites More sharing options...
JJohnsenDK Posted December 19, 2006 Author Share Posted December 19, 2006 Nice! and thanks for a great reply :D. I will try to work a little with the array into you post the example, looking forward to it :). Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/#findComment-144343 Share on other sites More sharing options...
HuggieBear Posted December 19, 2006 Share Posted December 19, 2006 I have a form that looks like this:[size=8pt][b]form.htm[/b][/size][code] <form name="upload" method="POST" action="process.php" enctype="multipart/form-data"> <input type="file" name="imagefile[]"><br> <input type="file" name="imagefile[]"><br> <input type="file" name="imagefile[]"><br> <input type="file" name="imagefile[]"><br> <input type="file" name="imagefile[]"><br> <input type="submit" name="submit" value="Upload"><br><br> </form>[/code]And the php that processes it like this:[size=8pt][b]process.php[/b][/size][code]<?php// If the form has been submitted then process itif(isset($_POST['submit'])){ // Perform this action for each of the uploaded files foreach ($_FILES['imagefile']['error'] as $k => $error){ if ($_FILES['imagefile']['error'][$k] == 0){ // Make sure the file is a jpeg if ($_FILES['imagefile']['type'][$k] == "image/jpeg" || $_FILES['imagefile']['type'][$k] == "image/pjpeg"){ // Copy the temporary file to the original file name copy($_FILES['imagefile']['tmp_name'][$k], "images/".$_FILES['imagefile']['name'][$k]) or die ("Could not copy"); echo "{$_FILES['imagefile']['name'][$k]}... Uploaded OK<br>\n"; // You could put your database insertion code here. } else { echo "Could Not Copy, Wrong Filetype ({$_FILES['imagefile']['type'][$k]})<br>"; } } }}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/#findComment-144350 Share on other sites More sharing options...
JJohnsenDK Posted December 20, 2006 Author Share Posted December 20, 2006 Looks great... But how do i updated my database? I have 5 colums a colum for each picture in the database. When i want to chance picture1 i update colum 1 how do i do this with your code? Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/#findComment-145339 Share on other sites More sharing options...
JJohnsenDK Posted December 20, 2006 Author Share Posted December 20, 2006 Anyone else who can help me out? Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/#findComment-145390 Share on other sites More sharing options...
craygo Posted December 20, 2006 Share Posted December 20, 2006 I just helped someone else out on this. Look at the code herehttp://www.phpfreaks.com/forums/index.php/topic,118784.15.htmlChange it accordinglyRay Quote Link to comment https://forums.phpfreaks.com/topic/31210-solved-uploading-several-filesimages/#findComment-145402 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.