jackie11 Posted January 19, 2007 Share Posted January 19, 2007 Hi allI am still having trouble with this form, I am looking to allow users to upload a picture to bd, and I'm looking to save that image to a folder (../Database/pics) and then insert the name of the image in a field in my db, I can successfully get the user to upload a picture but it stores their local path to the picture in the DB, I'm not sure how I can get around this any suggestions I have looked every where but can find any similar examplesThe code I'm using is:Form code: [code]<html><head> <title>form.php</title> <link rel="stylesheet" href="etc/TJConsultants.css"></head><body><br><div id="wrapper"><div id="header"><div style="text-align: center;"><br></div><br></div><div id="centercolumn"><div style="text-align: center;"><br></div><div style="text-align: center;"><hr style="width: 100%; height: 2px;"><font style="color: rgb(21, 27, 84);" size="+3">NewEmployee Entry Form<br></font><hr style="width: 100%; height: 2px;"><font style="color: rgb(21, 27, 84);" size="+3"><br></font><form method="post" action="script_1.php"><br> <table style="text-align: left; margin-left: auto; margin-right: auto; width: 490px; height: 495px;" border="1" cellpadding="2" cellspacing="2"> <tbody> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Surname:</td> <td><input name="Surname" size="40" type="text"></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Forename:</td> <td><input name="Forename" size="40" type="text"></td </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Job Title:</td> <td><input name="Job_title" size="40" type="text"></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">DOB:</td> <td><input name="DOB" size="40" type="text"></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Telephone:</td> <td><input name="Telephone" size="40" type="text"></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Email:</td> <td><input name="Email" size="40" type="text"></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Office Location:</td> <td><input name="Office_location" size="40" type="text"></td> </tr> <tr> <td style="text-align: left; font-family: Tahoma; color: rgb(0, 0, 102);">Expertise:</td> <td><textarea rows="2" name="Expertise" cols="35"></textarea></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Hobbies:</td> <td><textarea rows="2" name="Hobbies" cols="35"></textarea></td> </tr> <tr> <td style="font-family: Tahoma; color: rgb(0, 0, 102);"><br>Picture:<br> </td> <td><input name="Picture" size="35" type="file"></td> </tr> </tbody> </table> <br> <input value="Enter Record" type="submit"></form><br><br><hr style="width: 100%; height: 2px;"><div style="text-align: left;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 46px;" alt="Home" src="etc/Home_button.JPG"></a><br></div><hr style="width: 100%; height: 2px;"></div></div><div id="footer"><p style="text-align: left;"></p></div></div></body></html>[/code]My script code is:[code]<html><head> <title>Employee Index</title></head><body><h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1><hr style="width: 100%; height: 2px;"><?php $user="root";$host="localhost";$password="";$database = "employee_index";mysql_connect("localhost", "root", "") or die(mysql_error());mysql_select_db("employee_index") or die(mysql_error());$sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture)VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]','$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$_POST[Picture]')";$result = mysql_query($sqlquery)or die (mysql_error());mysql_close();print "<html><body><center>";print "<p><B>Thank You - You have just entered the following information successfully!</B><p>";print "Surname : $_POST[Surname]<br>";print "Forename : $_POST[Forename]<br>";print "Job Titile : $_POST[Job_title] <br>";print "Office Location : $_POST[Job_title]<br>";print "Telephone : $_POST[Telephone] <br>";print "Email :$_POST[Telephone] <br>";print "Expertise : $_POST[Expertise]<br>";print "Hobbies : $_POST[Hobbies] <br>";print "DOB : $_POST[DOB]<br>";print "Picture File: : $_POST[Picture]<br>";print "</body></html>";?><hr style="width: 100%; height: 2px;"><p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p><hr style="width: 100%; height: 2px;"></body></html>[/code]Any advice would be welcomedThanksJackie Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/ Share on other sites More sharing options...
ToonMariner Posted January 19, 2007 Share Posted January 19, 2007 you need attribute enctype="multipart/form-data" in your form tag.<form method="post" action="script_1.php" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164603 Share on other sites More sharing options...
papaface Posted January 19, 2007 Share Posted January 19, 2007 Not only that but you need to use $_FILES['picture'] instead of $_POST Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164604 Share on other sites More sharing options...
jackie11 Posted January 19, 2007 Author Share Posted January 19, 2007 Hi Thanks for the replies, I have made the both changes but it still does not work, the array appears in the db field ?Jackie Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164611 Share on other sites More sharing options...
simcoweb Posted January 19, 2007 Share Posted January 19, 2007 You're missing a whole bunch of code relative to the uploading and moving of the image.[code]<?php$target_path = "../Database/pics";$target_path = $target_path . basename( $_FILES['Picture']['name']); if(move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['Picture']['name']). " has been uploaded";} else{ echo "There was an error uploading the file, please try again!";}?>[/code]This moves the temporary image location (which is what you're getting) into a permanent location into your /pics folder. This does not provide any validation of the image or its extension. You should include that as well or someone could upload something ugly to your site. Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164613 Share on other sites More sharing options...
SharkBait Posted January 19, 2007 Share Posted January 19, 2007 Don't you also have to set the Form's max filesize as well? Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164623 Share on other sites More sharing options...
simcoweb Posted January 19, 2007 Share Posted January 19, 2007 Yeppers. That can be done in the form or/also in the PHP with 'if' validation. Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164631 Share on other sites More sharing options...
jackie11 Posted January 19, 2007 Author Share Posted January 19, 2007 Hi thereThanks for your help, I have added the suggested code and it saves a copy of the image to the directed file perfectly, however it is still not saving the file name in the db it just say array, not to sure why, any suggestions with this?Copy of my code now:[code]<html><head> <title>Employee Index</title></head><body><h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1><hr style="width: 100%; height: 2px;"><?php $user="root";$host="localhost";$password="";$database = "employee_index";mysql_connect("localhost", "root", "") or die(mysql_error());mysql_select_db("employee_index") or die(mysql_error());$sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture)VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]','$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$_FILES[Picture] ['name']')";$result = mysql_query($sqlquery)or die (mysql_error());mysql_close();print "<html><body><center>";print "<p><B>Thank You - You have just entered the following information successfully!</B><p>";print "Surname : $_POST[Surname]<br>";print "Forename : $_POST[Forename]<br>";print "Job Titile : $_POST[Job_title] <br>";print "Office Location : $_POST[Job_title]<br>";print "Telephone : $_POST[Telephone] <br>";print "Email :$_POST[Telephone] <br>";print "Expertise : $_POST[Expertise]<br>";print "Hobbies : $_POST[Hobbies] <br>";print "DOB : $_POST[DOB]<br>";print "Picture File: : $_FILES[Picture]<br>";print "</body></html>";$target_path = "../test_3/pics";$target_path = $target_path . basename( $_FILES['Picture']['name']); if(move_uploaded_file($_FILES['Picture']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['Picture']['name']). " has been uploaded";} else{ echo "There was an error uploading your picture file, please try again!";}?><hr style="width: 100%; height: 2px;"><p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p><hr style="width: 100%; height: 2px;"></body></html>[/code]RegardsJackie Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164633 Share on other sites More sharing options...
simcoweb Posted January 19, 2007 Share Posted January 19, 2007 There's a couple of things to do in order to get the actual name into the database. This should work:add this line into your code:[code]$imagename = basename($_FILES['Picture']['name']);[/code]Then in your INSERT query change the reference for the picture to simply: '$imagename'Basically what we've done is set a variable populated with the image's actual name. Then, in your query, it should use that for populating the database field. In your pages where you want the image to show you'd use a <img src tag like usual but like this:<img src='http://www.yoursitename.com/filedir/$imagename'> Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164676 Share on other sites More sharing options...
jackie11 Posted January 19, 2007 Author Share Posted January 19, 2007 HiI made the changes you suggested and it now works a treat, thanks for all your help, I would have been working on that for days!!!! :) :) :)Thanks againJackie Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164739 Share on other sites More sharing options...
simcoweb Posted January 19, 2007 Share Posted January 19, 2007 Cool! :)Now that you have the 'basic' functionality in there and it works, you should seriously consider some code to protect against malicious files being uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/#findComment-164748 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.