xProteuSx Posted December 6, 2008 Share Posted December 6, 2008 I have designed a database around the idea that I could upload multiple image files with a single form. Now, it seems, I can't figure out how to do this and all my hard work appear to be down the toilet atm. <tr valign="top"> <td bgcolor="#dedede" width="120" align="left"><font size="-1"> Image of Front: </font></td> <td bgcolor="#dedede" align="left"> <center><input type="file" name="notes_image_front" size="27"><br><font size="-2">Max filesize: 150kb. Width must be: 600px. Maximum height: 1500px.</center> </td> </tr> <tr valign="top"> <td bgcolor="#dedede" width="120" align="left"><font size="-1"> Image of Back: </font></td> <td bgcolor="#dedede" align="left"> <center><input type="file" name="notes_image_back" size="27"><br><font size="-2">Max filesize: 150kb. Width must be: 600px. Maximum height: 1500px.</center> </td> </tr> <tr valign="top"> <td bgcolor="#dedede" width="120" align="left"><font size="-1"> Thumbnail: </font></td> <td bgcolor="#dedede" align="left"> <center><input type="file" name="notes_image_thumb" size="27"><br><font size="-2">Max filesize: 50kb. Width must be: 200px. Maximum height: 500px.</center> </td> </tr> This is the form. The php for getting the three values is: $notes_image_front = mysql_real_escape_string($_POST['notes_image_front']); $notes_image_back = mysql_real_escape_string($_POST['notes_image_back']); $notes_image_thumb = mysql_real_escape_string($_POST['notes_image_thumb']); Ofcourse, when I go to submit the form, the three values come back as NULL. I have googled the crap out of 'how to upload multiple files with a single form in php' and have come up with nothing that I have been able to implement. Help please!! You guys have always come through for me before! Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/135775-upload-multiple-files-with-single-form/ Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Share Posted December 6, 2008 Are you posting the images into a blob database or a folder in your directory and just loading the path to mySQL? Quote Link to comment https://forums.phpfreaks.com/topic/135775-upload-multiple-files-with-single-form/#findComment-707497 Share on other sites More sharing options...
redarrow Posted December 6, 2008 Share Posted December 6, 2008 url http://uk.php.net/features.file-upload <form action="" method="post" enctype="multipart/form-data"> <p>Pictures: <input type="file" name="pictures[]" /> <input type="file" name="pictures[]" /> <input type="file" name="pictures[]" /> <input type="submit" value="Send" /> </p> </form> <?php foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135775-upload-multiple-files-with-single-form/#findComment-707501 Share on other sites More sharing options...
webmaster1 Posted December 6, 2008 Share Posted December 6, 2008 Here's a funtioning script I wrote to upload three files to a folder and write the path to a database. <?php if (isset($_POST['submit'])) //PROCESS THE FORM WHEN THE SUBMIT BUTTON IS PRESSED { //VALIDATE THE INDIVIDUAL FIELDS AND DEFINE THEM AS TRUE OR FALSE BASED ON THE USER'S INPUT //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if($filesize1==0) { $filesize1=FALSE; $message_filesize1="You forgot to enter this image file!"; //echo "$message_filesize1"; } else { $filesize1=TRUE; } //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if($filesize2==0) { $filesize2=FALSE; $message_filesize2="You forgot to enter this image file!"; //echo "$message_filesize2"; } else { $filesize2=TRUE; } //CHECK IF UPLOAD FILE FIELD 3 IS EMPTY $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if($filesize3==0) { $filesize3=FALSE; $message_filesize3="You forgot to enter this image file!"; //echo "$message_filesize3"; } else { $filesize3=TRUE; } //CHECK IF ANY THE FILES ARE THE SAME: $filenamecheck1= $HTTP_POST_FILES['ufile']['name'][0]; $filenamecheck2= $HTTP_POST_FILES['ufile']['name'][1]; $filenamecheck3= $HTTP_POST_FILES['ufile']['name'][2]; $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( (($filenamecheck1 == $filenamecheck2) || ($filenamecheck2 == $filenamecheck3) || ($filenamecheck1 == $filenamecheck3)) && ((!$filesize1==0 && isset($_POST['submit'])) && (!$filesize2==0 && isset($_POST['submit'])) && (!$filesize3==0 && isset($_POST['submit']))) ) { $filenamecheck_message = "one ore more of the files are the same"; //echo "$message_filenamecheck"; $message_filenamecheck = TRUE; $filenamecheck1= FALSE; $filenamecheck2= FALSE; $filenamecheck3= FALSE; } else { $message_filenamecheck = FALSE; $filenamecheck1= TRUE; $filenamecheck2= TRUE; $filenamecheck3= TRUE; } //CHECK IF FILE 1 IS NOT A *.JPEG OR *.GIF $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if ( ( ($HTTP_POST_FILES['ufile']["type"][0] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][0] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][0] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][0] < 50000) ) { $filetypecheck1=TRUE; } else { if (!($filesize1 == 0)) { $filetypecheck1=FALSE; $message_filetypecheck1="The first of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck1"; } } //CHECK IF FILE 2 IS NOT A *.JPEG OR *.GIF $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if ( ( ($HTTP_POST_FILES['ufile']["type"][1] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][1] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][1] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][1] < 50000) ) { $filetypecheck2=TRUE; } else { if (!($filesize2 == 0)) { $filetypecheck2=FALSE; $message_filetypecheck2="The second of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck2"; } } //CHECK IF FILE 3 IS NOT *.JPEG OR *.GIF $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( ( ($HTTP_POST_FILES['ufile']["type"][2] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][2] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][2] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][2] < 50000) ) { $filetypecheck3=TRUE; } else { if (!($filesize3 == 0)) { $filetypecheck3=FALSE; $message_filetypecheck3="The third of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck3"; } } //CHECK IF FILE 1 EXISTS ALREADY $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if ( (file_exists($path1)) && (!($filesize1 == 0)) ) { $fileexistcheck1=FALSE; $message_fileexistcheck1="The first file already exist on the server!"; //echo "$message_fileexistcheck1"; } else { $fileexistcheck1=TRUE; } //CHECK IF FILE 2 EXISTS ALREADY $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if ( (file_exists($path2)) && (!($filesize2 == 0)) ) { $fileexistcheck2=FALSE; $message_fileexistcheck2="The second file already exist on the server!"; //echo "$message_fileexistcheck2"; } else { $fileexistcheck2=TRUE; } //CHECK IF FILE 3 EXISTS ALREADY $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( (file_exists($path3)) && (!($filesize3 == 0)) ) { $fileexistcheck3=FALSE; $message_fileexistcheck3="The third file already exist on the server!"; //echo "$message_fileexistcheck3"; } else { $fileexistcheck3=TRUE; } //IF ALL INPUT FIELDS ARE DEFINED AS TRUE / VALIDATED if ( && $filesize1 && $filesize2 && $filesize3 && $filenamecheck1 && filenamecheck2 && filenamecheck3 && $filetypecheck1 && $filetypecheck2 && $filetypecheck3 && $fileexistcheck1 && $fileexistcheck2 && $fileexistcheck3) { //FIRST DEFINE PATHS AS VARIABLES $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; //NEXT POST THE IMAGES TO THE DESIGNATED FOLDER ON THE SERVER copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2); copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3); //PICK UP AND DEFINE INPUT AS INDIVIDUAL VARIABLES //CONNECT TO RELEVANT DATABASE //THE CONNECTION MUST PRECEDE THE mysql_real_escape_string FUNCTION. include("dbinfo.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database."); $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; //INSERT THE INPUT INTO DATABASE $query = "INSERT INTO test VALUES ('$path1','$path2','$path3')"; mysql_query($query); //NEXT DISPLAY A SUMMARY OF WHAT HAS BEEN UPLOADED echo "This item has been successfully submitted to the server.</br></br>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]." KB<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; //echo "<img src=\"$path1\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; //echo "<img src=\"$path2\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; //echo "<img src=\"$path3\" height=\"150\">"; echo "</br>It's best practice to confirm that your item has been submitted as desired</br></br> [place button here to link to view relevant section]</br></br> If you notice an error as a result of human input please find and delete the item indefinitely and reattempt its submission. Items should also be deleted as soon as they become redundant (e.g. vehichle sold)</br></br> Should you experience further technical difficulty please contact the web master:</br></br> [place button here to link to web master cms error log form] "; //VERY IMPORTANT! EXIT(); WILL NO LONGER DISPLAY THE FORM exit(); //CLOSE: IF ALL INPUT FIELDS TRUE: } //CLOSE: OVERALL VALIDATION: } ?> <HTML> <HEAD> </HEAD> <BODY> <!--BEGIN FORM AND SET IT TO PROCESS SELF AND HANDLE MULTIPART/FORM-DATA--> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <fieldset> <?php // IF ANY INDIVIDUAL INPUT FIELD IS DEFINED AS FALSE THEN DISPLAY ERROR MESSAGE if ($message_filesize2 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filenamecheck || $message_filetypecheck1 || $message_filetypecheck2 || $message_filetypecheck3 || $message_fileexistcheck1 || $message_fileexistcheck2 || $message_fileexistcheck3) // echo '*This item could not be processed because some of the information is missing or incorrect.</br></br>'; ?> <ol> <!--BEGIN FILE UPLOADS--> <li> <label for "ufile[]">Image File 1:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize1) echo ''.$message_filesize1.''; ?> <?php if ($message_filetypecheck1) echo ''.$message_filetypecheck1.''; ?> <?php if ($message_fileexistcheck1) echo ''.$message_fileexistcheck1.''; ?> </li> <li> <label for "ufile[]">Image File 2:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize2) echo ''.$message_filesize2.''; ?> <?php if ($message_filetypecheck2) echo ''.$message_filetypecheck2.''; ?> <?php if ($message_fileexistcheck2) echo ''.$message_fileexistcheck2.''; ?> </li> <li> <label for "ufile[]">Image File 3:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize3) echo ''.$message_filesize3.''; ?> <?php if ($message_filetypecheck3) echo ''.$message_filetypecheck3.''; ?> <?php if ($message_fileexistcheck3) echo ''.$message_fileexistcheck3.''; ?> </br> <?php if ($filenamecheck_message) echo ''.$filenamecheck_message.''; ?> </li> <li> <input name="submit" type="submit"> </li> </ol> </fieldset> </form> </BODY> </HTML> Its lengthy but does exactly what is says on the tin. Quote Link to comment https://forums.phpfreaks.com/topic/135775-upload-multiple-files-with-single-form/#findComment-707506 Share on other sites More sharing options...
xProteuSx Posted December 6, 2008 Author Share Posted December 6, 2008 redarrow, I did come across that snippet when I was searching the web for ideas. It seems nice and easy, but I can not figure out how to assign input1, input2, and input3 specific names, and also, I need the fields to upload in order. What I mean by this is that if someone fills out field1 and field3, and leaves field2 null, I can't have the file in field3 being uploaded to directory2. Know what I mean? Do you have any ideas for coding this? By the way, I'm not overly familiar with arrays. Thank you for your reply. webmaster, Wow. I will definitely take a look at your code. It will take some time to see whether I can make it work. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/135775-upload-multiple-files-with-single-form/#findComment-708007 Share on other sites More sharing options...
PFMaBiSmAd Posted December 6, 2008 Share Posted December 6, 2008 I need the fields to upload in order. What I mean by this is that if someone fills out field1 and field3, and leaves field2 null, I can't have the file in field3 being uploaded to directory2. Know what I mean? Do you have any ideas for coding this? Use an array (so you can use a foreach() loop for simple coding), but specify the array index names - <input type="file" name="pictures[notes_image_front]" /> <input type="file" name="pictures[notes_image_back]" /> <input type="file" name="pictures[notes_image_thumb]" /> The $key variable in the foreach() loop contains the array index name. Quote Link to comment https://forums.phpfreaks.com/topic/135775-upload-multiple-files-with-single-form/#findComment-708012 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.