mjgdunne Posted April 30, 2008 Share Posted April 30, 2008 Hi all, i have a form for uploading images into a mysql database, the form seems to work as the image does go to the database, my problem is how do i retrieve the image, when i query the database i only get a link, and my question is am i uploading the file correctly or do i need to change the code, any help or advice would be great thanks. Here is my upload form: <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false;} else {return true} } }function validate_form(thisform) { with (thisform) { if (validate_required(mymake,"Car Make must be selected!")==false) {mymake.focus();return false;} if (validate_required(mymodel,"Car Model must be filled out!")==false) {mymodel.focus();return false;} if (validate_required(myregistration,"Registration must be filled out!")==false) {myregistration.focus();return false;} } } </script> </head> <body> <form action="case.php" onsubmit="return validate_form(this);" method="post"> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Create new case</H1> </td></tr> <tr><td> <h2>Please upload a new picture</h2> <input type=hidden name=MAX_FILE_SIZE value=150000> <input type=hidden name=completed value=1> <br>Please choose an image to upload: <input type=file name=myimage><br> <br> Please enter the car make: <input name=mymake><br><br> Please enter the car model: <input name=mymodel><br><br> Please enter the car registration: <input name=myreg><br> <br> </TABLE> <TABLE BGCOLOR="#F0F8FF" WIDTH=100%> <TR><TD ALIGN="CENTER"> <input type="submit" value="Add Case"/><input type="reset" name="reset" value="Clear Form"/> </form><form action="login_success.php" method="post"> <input type=submit value="Home"></form></TD></tr> <TR><TD><BR></TD></TR> </table> </td></tr> </body> </html> Here is my code that im using to retrieve the image: <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>View Cases:</H1> <form method="post" action="view_2.php"> </td> </table> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <TD ALIGN=CENTER> <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="reff"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $result = mysql_query("SELECT * FROM reff ORDER BY make;"); $_SESSION['reff'] = array(); while ($row = mysql_fetch_assoc($result)) { $_SESSION['reff'][] = $row; } foreach ($_SESSION['reff'] as $row) { echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>ID</th>"; echo "<td>"; echo $row['pid']; echo "</td>"; echo "<tr><th>Make</th>"; echo "<td>"; echo $row['make']; echo "</td>"; echo "<tr><th>Model</th>"; echo "<td>"; echo $row['model']; echo "</td>"; echo "<tr><th>Registration</th>"; echo "<td>"; echo $row['registration']; echo "</td>"; echo "<tr><th>Image</th>"; echo "<td>"; echo $row['imgdata']; echo "</td>"; echo "<tr><th></th>"; echo "<td>"; echo "<button onclick=\"window.location.href='view_3.php?pid=".$row['pid']."'\">Details</button>"; echo "</td>"; echo "<tr>"; echo "<br>"; echo "<br>"; echo "</tr>"; } if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } if (mysql_num_rows($result) == 0) { echo "No records found"; exit; } ?> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> </form> <tr><form method="post" action="login_success.php"> <td align="center"><input type="submit" value="Home"/></td> </tr> </table> </table> </td> </tr> </table> </BODY> </HTML> Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/ Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 Are you saving the image in the database, or a link to the image? Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530284 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 Hi i am saving the images to a longblob in the database. Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530288 Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 OK, you will need to make php page that creates an image... So, you will need to pull the text from the database, then output the file: image.php <?php $id = $_GET['id']; $query = "SELECT * FROM databaseName WHERE id = '$id'"; $sql = mysql_query($query); if(mysql_num_rows($sql) > 0){ // Image was found header('Content-type: image/jpeg'); $row = mysql_fetch_array($sql); echo $row['myLongBlob']; }else{ // There was no image header('Content-type: image/jpeg'); readfile('/link/to/my/alterative.jpg'); } ?> Next you will need to get the file using HTML: ... <img src="image.php?id=1243" alt="My Image" /> ... it would all look something like the above (I didn't test the code, it may need some work). Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530307 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 Hi i am getting the error: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\view_2.php:19) in C:\xampp\htdocs\view_2.php on line 43 C:\Documents and Settings\Michael Dunne\My Documents\My Pictures\06-02-2008\DSC00064.JPG Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530333 Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 want to post everything from line 19 and above (if the file is short, just post the whole thing). Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530341 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 Hi this is from lines 19 - 46: <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <TD ALIGN=CENTER> <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="reff"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $pid = $_GET['pid']; $query = "SELECT * FROM reff WHERE pid = '$pid'"; $sql = mysql_query($query); if(mysql_num_rows($sql) > 0){ // Image was found header('Content-type: image/jpeg'); $row = mysql_fetch_array($sql); echo $row['imgdata']; }else{ Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530349 Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 the image.php file I posted above MUST but in its own file. Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530353 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 Hi, i have heard that it is not a good idea to store images in a database as it can slow it down etc, so i was told to save the upload image to a location on the server. I have a script as below: Here is the upload form: <form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file"> <input type="hidden" name="MAX_FILE_SIZE" value="5000000"> <input name="userfile" type="file"> <input type="submit" value="upload" name="file_uploaded"> </form> And here is the upload.php: <?php if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { $file_realname = $_FILES['userfile']['name']; copy($_FILES['userfile']['tmp_name'], realpath("upload").trim($file_realname)); } else { echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload."; } echo $file_realname; ?> This code does work, but i need to be able to assign it an picture id and save the picture id in the database along with the other details so when you query for results the image and other details will be displayed. Do you know how i would go about this? Link to comment https://forums.phpfreaks.com/topic/103555-help-with-images/#findComment-530368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.