systech44 Posted March 22, 2010 Share Posted March 22, 2010 Hello, I have a situation regarding the displaying the image from MySQL database. Here is my code of upload the images to MySQL. <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $conn = mysql_connect("localhost", "root", ""); mysql_select_db("test", $conn); $query = "INSERT INTO pics VALUES ('','$fileName', '$fileType', '$fileSize', '$content')"; mysql_query($query, $conn) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> <html> <head> <title>Pic Upload</title> </head> <form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> </html> Here is my code for the displaying the images from MySQL. //echo "Hello "; $errmsg = ""; if (! @mysql_connect("localhost","root","")) { $errmsg = "Cannot connect to database"; } @mysql_select_db("test"); $query = "SELECT content, type from pics where id = '1'"; $result = @MYSQL_QUERY($query); $data = @MYSQL_RESULT($result,0,"content"); $type = @MYSQL_RESULT($result,0,"type"); header("Content-type: $type"); echo $data; In this situation if I display the image without any string the it is displaying. But if I remove the comment from echo "Hello" then it is not working. I am trying to display the image in a <td></td> of table with info in other <td></td> with width amd height of <ing> tag specified. Please let me know what is the solution regarding the situation. Thanks a lot in advance. Quote Link to comment https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 22, 2010 Share Posted March 22, 2010 Each image displayed on a web page requires an <img src="URL_of_an_image" alt=""> tag - http://w3schools.com/html/html_images.asp This is basic html. The URL_of_an_image that you put into the <img tag would be to your .php script that retrieves the image from the database and output the content-type header followed by the image data. You cannot output image data directly on a web page. Quote Link to comment https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/#findComment-1030090 Share on other sites More sharing options...
skurai Posted March 22, 2010 Share Posted March 22, 2010 you would make it for example.... <img src="2ndfile.php?id=1" border="0" alt="" /> Quote Link to comment https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/#findComment-1030219 Share on other sites More sharing options...
systech44 Posted March 23, 2010 Author Share Posted March 23, 2010 Please let me know what is the solution. I like to show the images from database along with the relevant information side by side. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/#findComment-1030359 Share on other sites More sharing options...
dstar101 Posted March 23, 2010 Share Posted March 23, 2010 You can not store images in Database. First You need to create a new folder in your site root to store images.Then save the image name in Database and to display the image use the file name from DB example: define('GW_UPLOADPATH', 'images/'); fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $target = GW_UPLOADPATH . $tmpName; move_uploaded_file($_FILES['userfile']['tmp_name'], $target $conn = mysql_connect("localhost", "root", ""); mysql_select_db("test", $conn); $query = "INSERT INTO pics VALUES ('','$fileName', '$fileType', '$fileSize')"; mysql_query($query, $conn) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/#findComment-1030364 Share on other sites More sharing options...
skurai Posted March 23, 2010 Share Posted March 23, 2010 Dstar, you can store images in the DB as well. if($_FILES['file']['size'] > 0) { $fileName = $_FILES['file']['name']; $tmpName = $_FILES['file']['tmp_name']; $fileSize = $_FILES['file']['size']; $fileType = $_FILES['file']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO pictures (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error: Upload pictures failed.'); } then view the picture with viewimage.php?id=1 if(isset($_GET['id'])) { $id=$_GET['id']; $query = "select content, type from pictures where id=$id"; $result = mysql_query($query); $row = mysql_fetch_array($result); $data = $row['content']; $type = $row['type']; if ($type=="image/jpeg") $type = "jpeg"; Header( "Content-type: $type"); echo $data; } and you would show the image with <img src="viewimage.php?id=1" alt="" width="250" height="250"> and put the relevent data next to that image Quote Link to comment https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/#findComment-1030525 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.