RCS Posted April 13, 2008 Share Posted April 13, 2008 I created a script and just want to know if this is the best way to do this. The Script; OK the script that I created is made up of a form with 10 file upload spots, description, make and price. The script sends the information to my database for me to call on later to be displayed on a catalog page. What I want to know is does it make sense the way I wrote it or is their a better way or will this even work??? Your help is greatly appreciated. Thanks in advance. admin form form.php <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <blockquote> <p align="center"> </p> <p> <label><strong>Id:</strong> <input name="id" type="text" id="id" size="2" maxlength="2"> </label> (Pick id number 1 through 10) <p><label><b>Make:</b></label> <input type="text" name="make" id="make" /></p> <p><label><b>Price:</b></label> <input type="text" name="price" id="price" /></p> <p><label></label> <label><b>Description:</b></label> <textarea name="description" id="description" cols="45" rows="5"></textarea /> </p> <p><p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file" id="file" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file2" id="file2" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file3" id="file3" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file4" id="file4" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file5" id="file5" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file6" id="file6" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file7" id="file7" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file8" id="file8" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file9" id="file9" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file10" id="file10" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> </div> </blockquote> <blockquote><blockquote> </blockquote> </blockquote> </form> Array file array.php <?php $fileName = $_FILES['file']['name']; $tmpName = $_FILES['file']['tmpName']; $fileSize = $_FILES['file']['size']; $fileType = $_FILES['file']['type']; $fileName2 = $_FILES['file2']['name2']; $tmpName2 = $_FILES['file2']['tmpName2']; $fileSize2 = $_FILES['file2']['size2']; $fileType2 = $_FILES['file2']['type2']; $fileName3 = $_FILES['file3']['name3']; $tmpName3 = $_FILES['file3']['tmpName3']; $fileSize3 = $_FILES['file3']['size3']; $fileType3 = $_FILES['file3']['type3']; $fileName4 = $_FILES['file4']['name4']; $tmpName4 = $_FILES['file4']['tmpName4']; $fileSize4 = $_FILES['file4']['size4']; $fileType4 = $_FILES['file4']['type4']; $fileName5 = $_FILES['file5']['name5']; $tmpName5 = $_FILES['file5']['tmpName5']; $fileSize5 = $_FILES['file5']['size5']; $fileType5 = $_FILES['file5']['type5']; $fileName6 = $_FILES['file6']['name6']; $tmpName6 = $_FILES['file6']['tmpName6']; $fileSize6 = $_FILES['file6']['size6']; $fileType6 = $_FILES['file6']['type6']; $fileName7 = $_FILES['file7']['name7']; $tmpName7 = $_FILES['file7']['tmpName7']; $fileSize7 = $_FILES['file7']['size7']; $fileType7 = $_FILES['file7']['type7']; $fileName8 = $_FILES['file8']['name8']; $tmpName8 = $_FILES['file8']['tmpName8']; $fileSize8 = $_FILES['file8']['size8']; $fileType8 = $_FILES['file8']['type8']; $fileName9 = $_FILES['file9']['name9']; $tmpName9 = $_FILES['file9']['tmpName9']; $fileSize9 = $_FILES['file9']['size9']; $fileType9 = $_FILES['file9']['type9']; $fileName10 = $_FILES['file10']['name10']; $tmpName10 = $_FILES['file10']['tmpName10']; $fileSize10 = $_FILES['file10']['size10']; $fileType10 = $_FILES['file10']['type10']; The file that does all the work upload.php <?PHP if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/tif") || ($_FILES["file"]["type"] == "image/psd") || ($_FILES["file"]["type"] == "image/bmp") || ($_FILES["file"]["type"] == "image/pct") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file 1"; } if ((($_FILES["file2"]["type"] == "image/gif") || ($_FILES["file2"]["type"] == "image/jpeg") || ($_FILES["file2"]["type"] == "image/jpg") || ($_FILES["file2"]["type"] == "image/png") || ($_FILES["file2"]["type"] == "image/tif") || ($_FILES["file2"]["type"] == "image/psd") || ($_FILES["file2"]["type"] == "image/bmp") || ($_FILES["file2"]["type"] == "image/pct") || ($_FILES["file2"]["type"] == "image/pjpeg")) && ($_FILES["file2"]["size"] < 200000000000)) { if ($_FILES["file2"]["error"] > 0) { echo "Return Code: " . $_FILES["file2"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file2"]["name2"] . "<br />"; echo "Type: " . $_FILES["file2"]["type2"] . "<br />"; echo "Size: " . ($_FILES["file2"]["size2"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file2"]["tmp_name2"] . "<br />"; if (file_exists("upload/" . $_FILES["file2"]["name2"])) { echo $_FILES["file2"]["name2"] . " already exists. "; } else { move_uploaded_file($_FILES["file2"]["tmp_name2"], "upload/" . $_FILES["file2"]["name2"]); echo "Stored in: " . "upload/" . $_FILES["file2"]["name2"]; } } } else { echo "Invalid file 2"; } if ((($_FILES["file3"]["type"] == "image/gif") || ($_FILES["file3"]["type"] == "image/jpeg") || ($_FILES["file3"]["type"] == "image/jpg") || ($_FILES["file3"]["type"] == "image/png") || ($_FILES["file3"]["type"] == "image/tif") || ($_FILES["file3"]["type"] == "image/psd") || ($_FILES["file3"]["type"] == "image/bmp") || ($_FILES["file3"]["type"] == "image/pct") || ($_FILES["file3"]["type"] == "image/pjpeg")) && ($_FILES["file3"]["size"] < 200000000000)) { if ($_FILES["file3"]["error"] > 0) { echo "Return Code: " . $_FILES["file3"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file3"]["name3"] . "<br />"; echo "Type: " . $_FILES["file3"]["type3"] . "<br />"; echo "Size: " . ($_FILES["file3"]["size3"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file3"]["tmp_name3"] . "<br />"; if (file_exists("upload/" . $_FILES["file3"]["name3"])) { echo $_FILES["file3"]["name3"] . " already exists. "; } else { move_uploaded_file($_FILES["file3"]["tmp_name3"], "upload/" . $_FILES["file3"]["name3"]); echo "Stored in: " . "upload/" . $_FILES["file3"]["name3"]; } } } else { echo "Invalid file 3"; } if ((($_FILES["file4"]["type"] == "image/gif") || ($_FILES["file4"]["type"] == "image/jpeg") || ($_FILES["file4"]["type"] == "image/jpg") || ($_FILES["file4"]["type"] == "image/png") || ($_FILES["file4"]["type"] == "image/tif") || ($_FILES["file4"]["type"] == "image/psd") || ($_FILES["file4"]["type"] == "image/bmp") || ($_FILES["file4"]["type"] == "image/pct") || ($_FILES["file4"]["type"] == "image/pjpeg")) && ($_FILES["file4"]["size"] < 200000000000)) { if ($_FILES["file4"]["error"] > 0) { echo "Return Code: " . $_FILES["file4"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file4"]["name4"] . "<br />"; echo "Type: " . $_FILES["file4"]["type4"] . "<br />"; echo "Size: " . ($_FILES["file4"]["size4"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file4"]["tmp_name4"] . "<br />"; if (file_exists("upload/" . $_FILES["file4"]["name4"])) { echo $_FILES["file4"]["name4"] . " already exists. "; } else { move_uploaded_file($_FILES["file4"]["tmp_name4"], "upload/" . $_FILES["file4"]["name4"]); echo "Stored in: " . "upload/" . $_FILES["file4"]["name4"]; } } } else { echo "Invalid file 4"; } if ((($_FILES["file5"]["type"] == "image/gif") || ($_FILES["file5"]["type"] == "image/jpeg") || ($_FILES["file5"]["type"] == "image/jpg") || ($_FILES["file5"]["type"] == "image/png") || ($_FILES["file5"]["type"] == "image/tif") || ($_FILES["file5"]["type"] == "image/psd") || ($_FILES["file5"]["type"] == "image/bmp") || ($_FILES["file5"]["type"] == "image/pct") || ($_FILES["file5"]["type"] == "image/pjpeg")) && ($_FILES["file5"]["size"] < 200000000000)) { if ($_FILES["file5"]["error"] > 0) { echo "Return Code: " . $_FILES["file5"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file5"]["name5"] . "<br />"; echo "Type: " . $_FILES["file5"]["type5"] . "<br />"; echo "Size: " . ($_FILES["file5"]["size5"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file5"]["tmp_name5"] . "<br />"; if (file_exists("upload/" . $_FILES["file5"]["name5"])) { echo $_FILES["file5"]["name5"] . " already exists. "; } else { move_uploaded_file($_FILES["file5"]["tmp_name5"], "upload/" . $_FILES["file5"]["name5"]); echo "Stored in: " . "upload/" . $_FILES["file5"]["name5"]; } } } else { echo "Invalid file 5"; } if ((($_FILES["file6"]["type"] == "image/gif") || ($_FILES["file6"]["type"] == "image/jpeg") || ($_FILES["file6"]["type"] == "image/jpg") || ($_FILES["file6"]["type"] == "image/png") || ($_FILES["file6"]["type"] == "image/tif") || ($_FILES["file6"]["type"] == "image/psd") || ($_FILES["file6"]["type"] == "image/bmp") || ($_FILES["file6"]["type"] == "image/pct") || ($_FILES["file6"]["type"] == "image/pjpeg")) && ($_FILES["file6"]["size"] < 200000000000)) { if ($_FILES["file6"]["error"] > 0) { echo "Return Code: " . $_FILES["file6"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file6"]["name6"] . "<br />"; echo "Type: " . $_FILES["file6"]["type6"] . "<br />"; echo "Size: " . ($_FILES["file6"]["size6"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file6"]["tmp_name6"] . "<br />"; if (file_exists("upload/" . $_FILES["file6"]["name6"])) { echo $_FILES["file6"]["name6"] . " already exists. "; } else { move_uploaded_file($_FILES["file6"]["tmp_name6"], "upload/" . $_FILES["file6"]["name6"]); echo "Stored in: " . "upload/" . $_FILES["file6"]["name6"]; } } } else { echo "Invalid file 6"; } if ((($_FILES["file7"]["type"] == "image/gif") || ($_FILES["file7"]["type"] == "image/jpeg") || ($_FILES["file7"]["type"] == "image/jpg") || ($_FILES["file7"]["type"] == "image/png") || ($_FILES["file7"]["type"] == "image/tif") || ($_FILES["file7"]["type"] == "image/psd") || ($_FILES["file7"]["type"] == "image/bmp") || ($_FILES["file7"]["type"] == "image/pct") || ($_FILES["file7"]["type"] == "image/pjpeg")) && ($_FILES["file7"]["size"] < 200000000000)) { if ($_FILES["file7"]["error"] > 0) { echo "Return Code: " . $_FILES["file7"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file7"]["name7"] . "<br />"; echo "Type: " . $_FILES["file7"]["type7"] . "<br />"; echo "Size: " . ($_FILES["file7"]["size7"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file7"]["tmp_name7"] . "<br />"; if (file_exists("upload/" . $_FILES["file7"]["name7"])) { echo $_FILES["file7"]["name7"] . " already exists. "; } else { move_uploaded_file($_FILES["file7"]["tmp_name7"], "upload/" . $_FILES["file7"]["name7"]); echo "Stored in: " . "upload/" . $_FILES["file7"]["name7"]; } } } else { echo "Invalid file 7"; } if ((($_FILES["file8"]["type"] == "image/gif") || ($_FILES["file8"]["type"] == "image/jpeg") || ($_FILES["file8"]["type"] == "image/jpg") || ($_FILES["file8"]["type"] == "image/png") || ($_FILES["file8"]["type"] == "image/tif") || ($_FILES["file8"]["type"] == "image/psd") || ($_FILES["file8"]["type"] == "image/bmp") || ($_FILES["file8"]["type"] == "image/pct") || ($_FILES["file8"]["type"] == "image/pjpeg")) && ($_FILES["file8"]["size"] < 200000000000)) { if ($_FILES["file8"]["error"] > 0) { echo "Return Code: " . $_FILES["file8"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file8"]["name8"] . "<br />"; echo "Type: " . $_FILES["file8"]["type8"] . "<br />"; echo "Size: " . ($_FILES["file8"]["size8"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file8"]["tmp_name8"] . "<br />"; if (file_exists("upload/" . $_FILES["file8"]["name8"])) { echo $_FILES["file8"]["name8"] . " already exists. "; } else { move_uploaded_file($_FILES["file8"]["tmp_name8"], "upload/" . $_FILES["file8"]["name8"]); echo "Stored in: " . "upload/" . $_FILES["file8"]["name8"]; } } } else { echo "Invalid file 8"; } if ((($_FILES["file9"]["type"] == "image/gif") || ($_FILES["file9"]["type"] == "image/jpeg") || ($_FILES["file9"]["type"] == "image/jpg") || ($_FILES["file9"]["type"] == "image/png") || ($_FILES["file9"]["type"] == "image/tif") || ($_FILES["file9"]["type"] == "image/psd") || ($_FILES["file9"]["type"] == "image/bmp") || ($_FILES["file9"]["type"] == "image/pct") || ($_FILES["file9"]["type"] == "image/pjpeg")) && ($_FILES["file9"]["size"] < 200000000000)) { if ($_FILES["file9"]["error"] > 0) { echo "Return Code: " . $_FILES["file9"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file9"]["name9"] . "<br />"; echo "Type: " . $_FILES["file9"]["type9"] . "<br />"; echo "Size: " . ($_FILES["file9"]["size9"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file9"]["tmp_name9"] . "<br />"; if (file_exists("upload/" . $_FILES["file9"]["name9"])) { echo $_FILES["file9"]["name9"] . " already exists. "; } else { move_uploaded_file($_FILES["file9"]["tmp_name9"], "upload/" . $_FILES["file9"]["name9"]); echo "Stored in: " . "upload/" . $_FILES["file9"]["name9"]; } } } else { echo "Invalid file 9"; } if ((($_FILES["file10"]["type"] == "image/gif") || ($_FILES["file10"]["type"] == "image/jpeg") || ($_FILES["file10"]["type"] == "image/jpg") || ($_FILES["file10"]["type"] == "image/png") || ($_FILES["file10"]["type"] == "image/tif") || ($_FILES["file10"]["type"] == "image/psd") || ($_FILES["file10"]["type"] == "image/bmp") || ($_FILES["file10"]["type"] == "image/pct") || ($_FILES["file10"]["type"] == "image/pjpeg")) && ($_FILES["file10"]["size"] < 200000000000)) { if ($_FILES["file10"]["error"] > 0) { echo "Return Code: " . $_FILES["file10"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file10"]["name10"] . "<br />"; echo "Type: " . $_FILES["file10"]["type10"] . "<br />"; echo "Size: " . ($_FILES["file10"]["size10"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file10"]["tmp_name10"] . "<br />"; if (file_exists("upload/" . $_FILES["file10"]["name10"])) { echo $_FILES["file10"]["name10"] . " already exists. "; } else { move_uploaded_file($_FILES["file10"]["tmp_name10"], "upload/" . $_FILES["file10"]["name10"]); echo "Stored in: " . "upload/" . $_FILES["file10"]["name10"]; } } } else { echo "Invalid file 10"; } include("dbinfo.inc"); include("file_array.inc"); $con = mysql_connect($db_host, $db_user, $db_passwd) or die ("Could not connect to database."); mysql_select_db($db_name) or die ("Unable to connect to select database"); $query = "INSERT INTO upload(name, TYPE, size, content, make, description, price, name2, TYPE2, size2, content2, name3, TYPE3, size3, content3, name4, TYPE4, size4, content4, name5, TYPE5, size5, content5, name6, TYPE6, size6, content6, name7, TYPE7, size7, content7, name8, TYPE8, size8, content8, name9, TYPE9, size9, content9, name10, TYPE10, size10, content10,)". "VALUES ('$fileName', '$fileType', '$fileSize', '$content','$_POST[make]','$_POST[description]','$_POST[price]','$fileName2', '$fileType2', '$fileSize2', '$content2', '$fileName3', '$fileType3', '$fileSize3', '$content3', '$fileName4', '$fileType4', '$fileSize4', '$content4', '$fileName5', '$fileType5', '$fileSize5', '$content5', '$fileName6', '$fileType6', '$fileSize6', '$content6', '$fileName7', '$fileType7', '$fileSize7', '$content7', '$fileName8', '$fileType8', '$fileSize8', '$content8', '$fileName9', '$fileType9', '$fileSize9', '$content9', '$fileName10', '$fileType10', '$fileSize10', '$content10',)"; $result=mysql_query($query) or die ("Could not complete query."); ?> code tags added : Barand Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 Main question is will this work? before I spend all the time creating to database. 2) is their a better way to write this??? 3) will I get an error trying to process all the information to the database at once e.g 10 files?? 4) is the array I made correct??? 5) do have all correct info in form?? Can some please help me with these issues. Thanks Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 Why is no one helping me?? Quote Link to comment Share on other sites More sharing options...
marcus Posted April 13, 2008 Share Posted April 13, 2008 you tell us if you get any errors You could simplify your image type checking with an array $allowed = array('image/gif','image/jpg','image/png'); if(in_array($_FILES['bob']['type'],$allowed)){ return TRUE; }else { return FALSE: } Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 Not looking for someone to check for errors, just want to know if my logic is correct or should I be going about it in another way?? maybe an easier way to write the code. Thanks for the array tips. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 13, 2008 Share Posted April 13, 2008 Why is no one helping me?? Well, do you honestly expect people to read through all that code (especially since you didn't even take the time to use CODE tags so it would be more legible)? Perhaps you should break down the code to certain parts and ask specific questions. The only advice I will offer is as follows: 1) You are doing the EXACT same thing for each image. So why wuould you want to write out all of that code 10 times. Create a loop. By "copying and pasting" code in that manner you are asking for trouble. By using the same code each time if there are any errors you only need to correct them once. Example: <?php for ($fileNum=1; $fileNum<=10; $fileNum++) { $currentFile = $_FILES['file9'.$fileNum]; //Now include all the processing using the array $currentFile } ?> 2) Instead of a bunch of OR's <?php if ((($_FILES["file10"]["type"] == "image/gif") || ($_FILES["file10"]["type"] == "image/jpeg") || ($_FILES["file10"]["type"] == "image/jpg") || ($_FILES["file10"]["type"] == "image/png") || ($_FILES["file10"]["type"] == "image/tif") || ($_FILES["file10"]["type"] == "image/psd") || ($_FILES["file10"]["type"] == "image/bmp") || ($_FILES["file10"]["type"] == "image/pct") || ($_FILES["file10"]["type"] == "image/pjpeg")) && ($_FILES["file10"]["size"] < 200000000000)) { ?> Do this: <?php //Define this outside the loop $imgTypes = array ( 'image/gif','image/jpeg', 'image/jpg', 'image/png', 'image/tif', 'image/psd', 'image/bmp', 'image/pct', 'image/pjpeg'); //Put this inside the loop if (in_array($currentFile["type"], $imgTypes) && ($currentFile["size"] < 200000000000)) { ?> Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 okay I have now tested my script the way I wrote is and am getting an error Upload: 1.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: /tmp/phpLvc2TR Stored in: upload/1.jpgUpload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Upload: Type: Size: 0 Kb Temp file: already exists. Could not complete query. Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 Only one file gets uploaded and I get that error. Also none of the info is going to the database. I had this script working 100% with 1 file upload but after I added 9 more thats when I got the problem. Is thier a problem with my code in the query area of things? Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 I created this post at an attempt to explain my problem better. I created an upload form with these fields make, description, price, picture. The form then submits the information to the databse for me to call and display on a catalog page. The script is working great but now the issue I'm having is that I would like to be able to upload not just one file to database but ten using the same script. What is the easiest way to change the script to allow ten upload and not just one. Here is a sample of my script. The Form <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <p align="center"> </p> <p> <label><strong>Id:</strong> //INPUT FIELD FOR ID <input name="id" type="text" id="id" size="2" maxlength="2"> </label> (Pick id number 1 through 10) <p><label>Make:</label> //INPUT FIELD FOR MAKE OF PRODUCT <input type="text" name="make" id="make" /></p> <p><label>Price:</label> <input type="text" name="price" id="price" /></p> //INPUT FIELD FOR PRICE <p><label></label> <label>Description:</label> <textarea name="description" id="description" cols="45" rows="5"></textarea /> //INPUT FIELD FOR DESCRIPTION </p> <p><p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file" id="file" /> //INPUT FIELD FOR IMAGE <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <input type="submit" name="submit" value="Save" /> </form> File Array .INC File $fileName = $_FILES['file']['name']; //name of image $tmpName = $_FILES['file']['tmpName']; //Temp Directory $fileSize = $_FILES['file']['size']; //File Size $fileType = $_FILES['file']['type']; //File type Upload script $fileName = $_FILES['file']['name']; $tmpName = $_FILES['file']['tmpName']; $fileSize = $_FILES['file']['size']; $fileType = $_FILES['file']['type']; if ((($_FILES["file"]["type"] == "image/gif") //File types allowed || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/tif") || ($_FILES["file"]["type"] == "image/psd") || ($_FILES["file"]["type"] == "image/bmp") || ($_FILES["file"]["type"] == "image/pct") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000000)) //Max File Size { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . " "; } else { echo "Upload: " . $_FILES["file"]["name"] . " "; echo "Type: " . $_FILES["file"]["type"] . " "; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb "; echo "Temp file: " . $_FILES["file"]["tmp_name"] . " "; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; Quote Link to comment Share on other sites More sharing options...
RCS Posted April 13, 2008 Author Share Posted April 13, 2008 Can someone please help with this???? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 13, 2008 Share Posted April 13, 2008 I know you have maked this as solved, but now that you have posted the code properly I can now review it. Here is what I would do (may be some syntax errors): This would process all 10 images (I would also create a PHP loop for creating the form fields as well. Never copy-and-paste code that should be identical. <?php $imgTypes = array ( 'image/gif','image/jpeg', 'image/jpg', 'image/png', 'image/tif', 'image/psd', 'image/bmp', 'image/pct', 'image/pjpeg'); $for ($imgNum=1; $imgNum<=10; $imgNum++) { $image = $_FILES['file'+$imgNum]; $fileName = $image['name']; $tempName = $image['tmpName']; $fileSize = $image['size']; $fileType = $image['type']; $fileErr = $image['error']; if ($fileErr>0) { echo "Return Code: $fileErr\n"; } else { if (in_array($fileType, $imgTypes) && ($fileSize < 200000000000)) { echo "Upload: $fileName\n"; echo "Type: $fileType\n"; echo "Size: " . ($fileSize / 1024) . " Kb\n"; echo "Temp file: $tempName\n"; if (file_exists("upload/" . $fileName)) { echo "$fileName already exists.\n"; } else { move_uploaded_file($tempName, "upload/" . $fileName); echo "Stored in: " . "upload/" . $fileName; } } else { echo "Invalid file"; } } } ?> Quote Link to comment Share on other sites More sharing options...
RCS Posted April 14, 2008 Author Share Posted April 14, 2008 now I get an error Invalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid file and the only syntax error that I seen was should have been for and not $for not sure why I'm getting this error??? Invalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid fileInvalid file, all the files I'm trying to upload are valid type and size?? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 Havn't had time to read the whole thread, but the way you're storing the data in your database is just asking for trouble down the line. You should read up on database normalization. Here would be a good place to start. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 As for the errors - looks like they're caused becuase you're using a + sign to append the $imgNum onto the end of 'file' rather than a . Perhaps mjdamato was thinking in javascript. Try: <?php $imgTypes = array ( 'image/gif','image/jpeg', 'image/jpg', 'image/png', 'image/tif', 'image/psd', 'image/bmp', 'image/pct', 'image/pjpeg'); for ($imgNum=1; $imgNum<=10; $imgNum++) { $image = $_FILES['file'.$imgNum]; $fileName = $image['name']; $tempName = $image['tmpName']; $fileSize = $image['size']; $fileType = $image['type']; $fileErr = $image['error']; if ($fileErr>0) { echo "Return Code: $fileErr\n"; } else { if (in_array($fileType, $imgTypes) && ($fileSize < 200000000000)) { echo "Upload: $fileName\n"; echo "Type: $fileType\n"; echo "Size: " . ($fileSize / 1024) . " Kb\n"; echo "Temp file: $tempName\n"; if (file_exists("upload/" . $fileName)) { echo "$fileName already exists.\n"; } else { move_uploaded_file($tempName, "upload/" . $fileName); echo "Stored in: " . "upload/" . $fileName; } } else { echo "Invalid file"; } } } ?> Quote Link to comment Share on other sites More sharing options...
RCS Posted April 14, 2008 Author Share Posted April 14, 2008 I'm now getting this error Invalid fileUpload: 2.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/2.jpgUpload: 3.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/3.jpgUpload: 4.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/4.jpgUpload: 5.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/5.jpgUpload: 6.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/6.jpgUpload: 7.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/7.jpgUpload: 8.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/8.jpgUpload: 9.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/9.jpgUpload: 10.jpg Type: image/pjpeg Size: 13.0771484375 Kb Temp file: Stored in: upload/10.jpg And none of the images are saved, another question that I have would be how would I also add all the info for the uploaded files to the database??? Not sure why the error everything look to be ok?????? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 Can you post your updated code please? Quote Link to comment Share on other sites More sharing options...
RCS Posted April 14, 2008 Author Share Posted April 14, 2008 OK well this is the form <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <blockquote> <p align="center"> </p> <p> <label><strong>Id:</strong> <input name="id" type="text" id="id" size="2" maxlength="2"> </label> (Pick id number 1 through 10) <p><label><b>Make:</b></label> <input type="text" name="make" id="make" /></p> <p><label><b>Price:</b></label> <input type="text" name="price" id="price" /></p> <p><label></label> <label><b>Description:</b></label> <textarea name="description" id="description" cols="45" rows="5"></textarea /> </p> <p><p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file" id="file" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <label for="file"><strong>Filename:</strong></label> <input type="file" name="file2" id="file2" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong> </p> <div align="center"><br /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file3" id="file3" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <label for="file"><strong>Filename:</strong></label> <input type="file" name="file4" id="file4" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong> </p> <div align="center"><br /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file5" id="file5" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <label for="file"><strong>Filename:</strong></label> <input type="file" name="file6" id="file6" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong> </p> <div align="center"><br /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file7" id="file7" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <label for="file"><strong>Filename:</strong></label> <input type="file" name="file8" id="file8" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong> </p> <div align="center"><br /> </p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file9" id="file9" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> </p> <div align="center"><br /> <label for="file"><strong>Filename:</strong></label> <input type="file" name="file10" id="file10" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> </div> </form> I have not yet remade my action script with database info, I tried yours. <?php $imgTypes = array ( 'image/gif','image/jpeg', 'image/jpg', 'image/png', 'image/tif', 'image/psd', 'image/bmp', 'image/pct', 'image/pjpeg'); for ($imgNum=1; $imgNum<=10; $imgNum++) { $image = $_FILES['file'.$imgNum]; $fileName = $image['name']; $tempName = $image['tmpName']; $fileSize = $image['size']; $fileType = $image['type']; $fileErr = $image['error']; if ($fileErr>0) { echo "Return Code: $fileErr\n"; } else { if (in_array($fileType, $imgTypes) && ($fileSize < 200000000000)) { echo "Upload: $fileName\n"; echo "Type: $fileType\n"; echo "Size: " . ($fileSize / 1024) . " Kb\n"; echo "Temp file: $tempName\n"; if (file_exists("upload/" . $fileName)) { echo "$fileName already exists.\n"; } else { move_uploaded_file($tempName, "upload/" . $fileName); echo "Stored in: " . "upload/" . $fileName; } } else { echo "Invalid file"; } } } ?> Quote Link to comment Share on other sites More sharing options...
xylex Posted April 14, 2008 Share Posted April 14, 2008 I didn't get a chance to play around with your code at all, but the file's type property is provided client side and can be spoofed, so it should never be solely relied on for determining the image type. With the code how it is written, a malicious user would be able to upload hack.php, declare it a mime type of jpeg when it's sent to your server, and that file would be accpeted and moved to the upload/ directory. There's a bunch of posts about better ways to check for this if you search, so I'm not going to repeat it here, but take a look at them. Quote Link to comment Share on other sites More sharing options...
RCS Posted April 14, 2008 Author Share Posted April 14, 2008 OK I see what you saying but in my case it's all in an admin area that is protected and I am the only one that wil be uploading images. I really badly need to get this working, I would appreciated it very much if you could help, I have been trying to do this for like two weeks lol. It works fine this way but I need to be able to add 9 more images with each item. Please help if you can. My current script that works with one image is like this. The Form <?php <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <p align="center"> </p> <p> <p><label for="file"><strong>Filename:</strong></label> <input type="file" name="file" id="file" /> <input type="hidden" name="MAX_FILE_SIZE" value="100000000000000"> <strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p> <p><label><b>Make:</b></label> <input type="text" name="make" id="make" /></p> <p><label><b>Price:</b></label> <input type="text" name="price" id="price" /></p> <p><label></label> <label><b>Description:</b></label> <textarea name="description" id="description" cols="45" rows="5"></textarea /> </p> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> </div> </form> ?> Array File <?php $fileName = $_FILES['file']['name']; $tmpName = $_FILES['file']['tmpName']; $fileSize = $_FILES['file']['size']; $fileType = $_FILES['file']['type']; ?> Upload Script <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/tif") || ($_FILES["file"]["type"] == "image/psd") || ($_FILES["file"]["type"] == "image/bmp") || ($_FILES["file"]["type"] == "image/pct") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } include("dbinfo.inc"); include("file_array.inc"); $con = mysql_connect($db_host, $db_user, $db_passwd) or die ("Could not connect to database."); mysql_select_db($db_name) or die ("Unable to connect to select database"); $query = "INSERT INTO upload(name, TYPE, size, content, make, description, price)". "VALUES ('$fileName', '$fileType', '$fileSize', '$content','$_POST[make]','$_POST[description]','$_POST[price]')"; $result=mysql_query($query) or die ("Could not complete query."); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2008 Share Posted April 14, 2008 Let's start with the form. Your first file element has the name "file" and the rest are "file2", "file3", etc. The first one should be called "file1". As I stated previously you should use a loop to create the form fields instead of the copy-paste method of programming. Your HTML has a lot of errors as well - many opening tags with no closing tags and vice-versa. You have a MAX_FILE_SIZE hidden filed, but it is not used anywhere. If you do want to use it you either need to declare it one, or if you need different values for each file, then you need to append a number to each one. No offense, but it is this kind of slopiness that makes it difficult to solve otherwise simple problems. Try this for your form: <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <blockquote> <p align="center"> </p> <p> <label><strong>Id:</strong> <input name="id" type="text" id="id" size="2" maxlength="2"> </label> (Pick id number 1 through 10) <p><label><b>Make:</b></label> <input type="text" name="make" id="make" /></p> <p><label><b>Price:</b></label> <input type="text" name="price" id="price" /></p> <p><label></label> <label><b>Description:</b></label> <textarea name="description" id="description" cols="45" rows="5"></textarea /> </p> <?php for ($imgNum=1; $imgNum<=10; $imgNum++) { echo "<p><label for=\"file{$imgNum}\"><strong>Filename:</strong></label>\n"; echo "<input type=\"file\" name=\"file{$imgNum}\" id=\"file{$imgNum}\" />\n"; echo "<strong>(Note pictures must be sized to 182 X 124 pixels)</strong></p>\n"; } ?> <div align="center"><br /> <input type="submit" name="submit" value="Save" /> </p> </div> </form> Quote Link to comment Share on other sites More sharing options...
RCS Posted April 14, 2008 Author Share Posted April 14, 2008 OK now tried to use the code below to process the info to test if it would upload the files, it says that everything uploaded ok but no files are in the upload directory. <?php $imgTypes = array ( 'image/gif','image/jpeg', 'image/jpg', 'image/png', 'image/tif', 'image/psd', 'image/bmp', 'image/pct', 'image/pjpeg'); for ($imgNum=1; $imgNum<=10; $imgNum++) { $image = $_FILES['file'.$imgNum]; $fileName = $image['name']; $tempName = $image['tmpName']; $fileSize = $image['size']; $fileType = $image['type']; $fileErr = $image['error']; if ($fileErr>0) { echo "Return Code: $fileErr\n"; } else { if (in_array($fileType, $imgTypes) && ($fileSize < 200000000000)) { echo "Upload: $fileName\n"; echo "Type: $fileType\n"; echo "Size: " . ($fileSize / 1024) . " Kb\n"; echo "Temp file: $tempName\n"; if (file_exists("upload/" . $fileName)) { echo "$fileName already exists.\n"; } else { move_uploaded_file($tempName, "upload/" . $fileName); echo "Stored in: " . "upload/" . $fileName; } } else { echo "Invalid file"; } } } ?> I'm still not sure how I would ssend all the info to the database, this was just a test to see if it would upload. Quote Link to comment Share on other sites More sharing options...
RCS Posted April 14, 2008 Author Share Posted April 14, 2008 Any ideas, are you able to help?? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2008 Share Posted April 14, 2008 Any ideas, are you able to help?? Dude, this is a forum. I don't run an RSS feed to my computer 24/7 to answer a question the moment it is posted. Have you tried any debugging processes? move_uploaded_file() will return false if the file it is trying to upload is not valid. I would start will adding some debugging there. Change this move_uploaded_file($tempName, "upload/" . $fileName); To this if (move_uploaded_file($tempName, "upload/" . $fileName)) { echo "Stored in: upload/{$fileName}<br />"; } else { echo "Error uploading file {$tempName}<br />"; } Im guessing that the $tempfile name and location are not correct because of this: $tmpName = $_FILES['file']['tmpName']; Try this $tmpName = $_FILES['file']['tmp_name']; The reason your original code worked was that you never used the variables you created! You created the $tmpName variable, but then in the move_uploaded_file() function you used $_FILES["file"]["tmp_name"]. Why create the variable if you are not going to use it? This is just some constructive criticism, but you really need to plan out your code. As I stated about the HTML there were a lot of orphan tags, there are PHP variables that were created and not used, etc. As the programmer you need to know exactly what variables are created and where they are used. Quote Link to comment 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.