cgskinner Posted October 12, 2009 Share Posted October 12, 2009 Hi, I'm new to the PHP world. I'm using a book example for an image upload and view and for some reason it's not working. I've done this example before and wasn't sure if I had to make some mods to it to make it work (some book examples had errors). I can't go back to see my old code because I failed to back it up and mutilated it with the following book example (that I couldn’t get to due to the increase in my workload). Below is the HTML & PHP code I’m using. The main issue is that it’s not writing to the database and saving the file to the directory. I also have the PHP for the DB table creation if needed. HTML: <html> <head> <title>Upload your pic to our site!</title> </head> <body> <form name="form1" method="post" action="check_image.php" enctype="multipart/form-data"> <table border="0" cellpadding="5"> <tr> <td>Image Title or Caption<br /> <em>Example: You talkin' to me?</em></td> <td><input name="image_caption" type="text" id="image_caption" size="55" maxlength="255" /></td> </tr> <tr> <td>Your Username</td> <td><input name="image_username" type="text" id="image_username" size="15" maxlength="255" /></td> </tr> <td>Upload Image:</td> <td><input name="image_filename" type="file" id="image_filename" /></td> </tr> </table> <br /> <em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em> <p align="center"><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Clear Form" /> </p> </form> </body> </html> PHP: <?php //connect to database $link = mysql_connect("localhost", "dir", "pass") or die("Could not connect: " . mysql_error()); mysql_select_db("moviesite", $link) or die(mysql_error()); //make vars available $image_caption = $_POST['image_caption']; $image_username = $_POST['image_username']; $image_tempname = $_FILES['image_filename']['name']; $today = date("Y-m-d"); //upload image & check for image type $ImageDir = "images/"; $ImageName = $ImageDir . $image_tempname; if(move_uploaded_file($_FILES['image_filename']['tmp_mane'], $ImageName)){ //get info about the image being uploaded list($width, $height, $type, $attr) = getimagesize($ImageName); switch($type){ case 1: $ext = ".gif"; break; case 2: $ext = ".jpg"; break; case 3: $ext = ".png"; break; default: echo "Sorry, but the file you uploaded was not a GIF, JPG or PNG file.<br>"; echo "Please hit the BACK button and try again."; } //insert info into image table $insert = "INSERT INTO images (image_caption, image_username, image_date) VALUES ('$image_caption', '$image_username', '$today')"; $insertresults = mysql_query($insert) or die(mysql_error()); $lastpicid = mysql_insert_id(); $newfilename = $ImageDir . $lastpicid . $ext; rename($ImageName, $newfilename); } ?> <html> <head> <title>Here is your pic!</title> </head> <body> <h1>So how does it feel to be famous?</h1><br /><br /> <p>Here is the picture you just uploaded to our servers:</p> <img src="images/<?php echo $lastpicid . $ext; ?>" align="left" /> <strong><?php echo $image_name; ?></strong><br /> This image is a <?php echo $ext; ?> image.<br /> It is <?php echo $width; ?> pixels wide and <?php $height; ?> pixels high."<br> It was uploaded on <?php echo $today; ?>. </body> </html> Any solutions will be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/ Share on other sites More sharing options...
Alex Posted October 12, 2009 Share Posted October 12, 2009 Can you be more specific about what's not working? Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935526 Share on other sites More sharing options...
cgskinner Posted October 12, 2009 Author Share Posted October 12, 2009 The main issue is that it’s not writing to the database and saving the file to the directory. So, in turn does not display the image nor the image parameters. I've tested the old files before making the change, and it saved the file and created entries into the DB just fine. Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935575 Share on other sites More sharing options...
mikesta707 Posted October 12, 2009 Share Posted October 12, 2009 try changing this if(move_uploaded_file($_FILES['image_filename']['tmp_mane'], $ImageName)){ //get info about the image being uploaded list($width, $height, $type, $attr) = getimagesize($ImageName); switch($type){ case 1: $ext = ".gif"; break; case 2: $ext = ".jpg"; break; case 3: $ext = ".png"; break; default: echo "Sorry, but the file you uploaded was not a GIF, JPG or PNG file.<br>"; echo "Please hit the BACK button and try again."; } to this, to see if there is a problem with moving the file if(move_uploaded_file($_FILES['image_filename']['tmp_mane'], $ImageName)){ //get info about the image being uploaded list($width, $height, $type, $attr) = getimagesize($ImageName); switch($type){ case 1: $ext = ".gif"; break; case 2: $ext = ".jpg"; break; case 3: $ext = ".png"; break; default: echo "Sorry, but the file you uploaded was not a GIF, JPG or PNG file.<br>"; echo "Please hit the BACK button and try again."; } else { echo "FIle upload failed!"; exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935580 Share on other sites More sharing options...
cgskinner Posted October 12, 2009 Author Share Posted October 12, 2009 Give me a "parse error". I think it's because of the }else{ is off of the switch() not the if(). So, I moved it to the end of the if() and of course when I run it, it states that the "File upload failed!". Which is what I'm trying to get resolved. It did work for the prev file(s) before I went back to review. But for some reason it's not. Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935592 Share on other sites More sharing options...
cgskinner Posted October 12, 2009 Author Share Posted October 12, 2009 I just tested and compaired what was left of the old code with the new. the old code works fine as far as making entries to the DB and saving the file (file mod feature doesn't work, but thats nither here nor there). Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935604 Share on other sites More sharing options...
lemmin Posted October 12, 2009 Share Posted October 12, 2009 Try changing "mane" to "name." if(move_uploaded_file($_FILES['image_filename']['tmp_mane'], $ImageName)){ Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935610 Share on other sites More sharing options...
cgskinner Posted October 12, 2009 Author Share Posted October 12, 2009 LOL!!! It still doesn't work like it supposed to but, those are different problems that I think I can handle. Much obliged. Dyslexia will be the death of me. Quote Link to comment https://forums.phpfreaks.com/topic/177432-solved-image-upload-and-view/#findComment-935617 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.