Jacbey Posted August 29, 2011 Share Posted August 29, 2011 Hi Guys, I'm having a problem with uploading a file. I want it to upload the photo then put the link into the database, but I need it to upload the picture into two directories. I have written the php for it but it keeps coming up with "Invalid File" which is in the else clause at the end of the whole thing. Here is the code so far, including the form and database stuff. <?php session_start(); ?> <td background="../images/middle.png" width="1124" height="" alt="Middle"> <table align="center"> <tr> <td> <table align="center" cellpadding="5"> <?php $admin_session = $_SESSION['admin_login']; $username = $_REQUEST['username']; $password = $_REQUEST['password']; if ($admin_session == NULL) { echo "Sorry, wrong username or password. You will be redirected in 5 seconds."; echo "<meta http-equiv='refresh' content='5;url=../admin_login.php?access=error'>"; } else { $myServer = "server"; $myUser = "user"; $myPass = "password"; $myDB = "database"; //connection to the database $dbhandle = mysql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); //select a database to work with $selected = mysql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); $query = "SELECT * FROM item WHERE show_admin = '1' "; $result = mysql_query($query) or die('Error Getting Information Requested'); $row = mysql_fetch_array($result); $username = $row['username'] ; $num = mysql_num_rows($result); $item_list = mysql_result($result,$i,"item_id"); echo "<br /> <br />" ; echo "<br /> <br />" ; if (!(isset($_POST['item_change_submit']))) { echo "<tr> <td> Name: </td> <td> Description: </td> <td> Photo: </td> <td> Price(£): </td> <td> Number of <br />views: </td> <td> To show <br />in shop: </td> <td> Category </td> <td> Grade </td> <td> Size </td> <td> Update Item </td> </tr>"; $i = 0; while ($i < $num) { $f1 = " <form action='' method='post'> <input type='hidden' name='item_id' id='item_id' value='" . mysql_result($result,$i,"item_id") . "' /> <tr> <td> <textarea rows='5' cols='10' name='item_name' id='item_name'>" . mysql_result($result,$i,"item_name") . "</textarea> </td> <td> <textarea rows='5' cols='10' name='description' id='description'>" . mysql_result($result,$i,"description") . "</textarea> </td> <td> <table align='center'> <tr> <td> <img src='" . mysql_result($result,$i,"photo") . "' width='150' height='150' /> </td> </tr> <tr> <td> <input type='file' name='photo' id='photo' style='width:175px;'/> </td> </tr> </table> </td> <td> <input type='text' name='price' id='price' value='" . mysql_result($result,$i,"price") . "' style='width:50px;'/> </td> <td> <input type='text' name='views' id='views' readonly='readonly' value='" . mysql_result($result,$i,"item_views") . "' style='width:50px;'/> </td> <td> <input type='text' name='to_show' id='to_show' value='" . mysql_result($result,$i,"to_show") . "' style='width:50px;'/> </td> <td> <input type='text' name='category' id='category' readonly='readonly' value='" . mysql_result($result,$i,"category") . "' style='width:50px;'/> </td> <td> <input type='text' name='grade' id='grade' readonly='readonly' value='" . mysql_result($result,$i,"grade") . "' style='width:50px;'/> </td> <td> <input type='text' name='size' id='size' value='" . mysql_result($result,$i,"size") . "' style='width:75px;'/> </td> <td> <input type='submit' name='item_change_submit' id='item_change_submit' value='Update Item.' /> </td> </form> </tr> <tr> <td height='50'> </td> </tr>"; echo "" . $f1 ."" ; $i++; } } else { if($_POST['photo'] == "") $photo_update = "images/item_pictures/default_item.png"; else { if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000)) { 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 />"; //Check image doesn't exist in first folder then uploads if doesn't. if (file_exists("images/item_pictures/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "images/item_pictures/" . $_FILES["file"]["name"]); echo "Stored in: " . "images/item_pictures/" . $_FILES["file"]["name"]; } //Check image doesn't exist in second*admin*folder then uploads if doesn't. if (file_exists("admin/images/item_pictures/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "admin/images/item_pictures/" . $_FILES["file"]["name"]); $photo_update = "admin/images/item_pictures/" . $_FILES["file"]["name"]; echo "Stored in: " . $uploaded_file; } } } else { echo "Invalid file"; } } $item_update_query = "UPDATE item SET item_name = '" . $_POST['item_name'] . "', description = '" . $_POST['description'] . "', photo = '" . $photo_update . "', price = '" . $_POST['price'] . "', to_show = '" . $_POST['to_show'] . "', size = '" . $_POST['size'] . "' WHERE item_id = '" . $_POST['item_id'] . "'"; if (mysql_query($item_update_query)){ echo "The item " . $_POST['item_name'] . " was changed with the following information: "; echo "<br /><br />"; echo "<tr> <td> <textarea readonly='readonly' rows='5' cols='10' name='item_name' id='item_name'>" . $_POST['item_name'] . "</textarea> </td> <td> <textarea readonly='readonly' rows='5' cols='10' name='description' id='description'>" . $_POST['description'] . "</textarea> </td> <td> <td> <img src='" . $photo_update . "' width='150' height='150' /> </td> </td> <td> <input type='text' readonly='readonly' name='price' id='price' value='" . $_POST['price'] . "' style='width:50px;'/> </td> <td> <input type='text' readonly='readonly' name='views' id='views' readonly='readonly' value='" . $_POST['views'] . "' style='width:50px;'/> </td> <td> <input type='text' readonly='readonly' name='to_show' id='to_show' value='" . $_POST['to_show'] . "' style='width:50px;'/> </td> <td> <input type='text' readonly='readonly' name='category' id='category' readonly='readonly' value='" . $_POST['category'] . "' style='width:50px;'/> </td> <td> <input type='text' readonly='readonly' name='grade' id='grade' readonly='readonly' value='" . $_POST['grade'] . "' style='width:50px;'/> </td> <td> <input type='text' readonly='readonly' name='size' id='size' value='" . $_POST['size'] . "' style='width:75px;'/> </td> </tr>"; } } } ?> </table> </td> </tr> </table> </td> </tr> Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/ Share on other sites More sharing options...
phpWilliam Posted August 29, 2011 Share Posted August 29, 2011 Hi Jacbey, try adding a enctype="multipart/form-data" to the <form> tag: $f1 = "<form action='' enctype='multipart/form-data' method='post'>"; $f1 .= {rest of code} This should then allow files to be sent via $_FILES Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/#findComment-1263127 Share on other sites More sharing options...
Jacbey Posted August 29, 2011 Author Share Posted August 29, 2011 thanks William However, it doesn't upload the file and it just gives me the default picture it should show so I have no idea what's wrong with it and it's not giving me any errors anymore. Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/#findComment-1263128 Share on other sites More sharing options...
phpWilliam Posted August 29, 2011 Share Posted August 29, 2011 Ah, where your if statement is checking if $_POST['photo'] == "" I would have probably used if($_FILES['photo']['size']==0) to verify if a file had been chosen (or in your logical case the opposite). Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/#findComment-1263131 Share on other sites More sharing options...
Jacbey Posted August 29, 2011 Author Share Posted August 29, 2011 Cool, I'll implement that, I figured out what it was, I hadn't changed $_FILES['file'] to $_FILES['photo'] that was why it wasn't working Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/#findComment-1263132 Share on other sites More sharing options...
phpWilliam Posted August 29, 2011 Share Posted August 29, 2011 I've also noticed, your php code refers too: $_FILES["file"] however, your html has <input type='file' name'photo' /> Your HTML code would produced a $_FILES['photo'] array if that makes sense. i'd either: change the php code to use $_FILES["photo"] instead of $_FILES["file"] or, Rename the file input to have a name='file' Hope this helps, Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/#findComment-1263133 Share on other sites More sharing options...
Jacbey Posted August 29, 2011 Author Share Posted August 29, 2011 Brilliant, thanks William, it works like a charm now One more thing, if you know how? D'you know how to get the image dimensions? Because I want to write them into the database as well thanks, Link to comment https://forums.phpfreaks.com/topic/245949-double-file-upload/#findComment-1263137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.