Bopo Posted April 1, 2009 Share Posted April 1, 2009 Hey Well I have written a little script to pull an image from a DB for me (yeah I know its bad practice to store images as blobs in db's, but it will only ever be 1 image) Anyway my problem is I'm using sessions, and to display the image I use a header, obviously headers have to appear at the top of a page, but so does my session and I want to display this image within the middle of static content on my page, using an include statement, here's what I have <?php include ("blogconnect.php"); $sql = "SELECT * FROM userimage"; $query = mysql_query($sql, $connect) or die(mysql_error()); if(mysql_num_rows($query)== 1) { $row = mysql_fetch_assoc($query); $userimage = $row['image']; header('Content-type: image/jpeg'); echo $userimage; } else { echo '<p><img src="../images/profile_blank_gray.jpg" width="75" height="75" alt="annyomous image" /> Upload an image</p>'; } ?> To be honest I'm looking for a method that doesn't need to output the image using a header. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2009 Share Posted April 1, 2009 I think you are confusing the difference of including an image on a page (i.e. using the IMG tags) versus handing the image to the browser as an image. The code you have will either "serve" the image to the browser as a file or try to insert an image tag intothe HTML page - two entirely different things. In the page that you want to "display" the image, do somethign like this: <p>Here is some text that comes before the image</p> <br /> <img src="getuserimage.php"> <br /> <p>Some text that comes after the image</p> Then create the page "getuserimage.php" so it will serve the image to the browser. <?php include ("blogconnect.php"); $sql = "SELECT * FROM userimage"; $query = mysql_query($sql, $connect) or die(mysql_error()); if(mysql_num_rows($query)== 1) { $row = mysql_fetch_assoc($query); $img_content = $row['image']; } else { $img_content = readfile("../images/profile_blank_gray.jpg"); } header('Content-type: image/jpeg'); echo $img_content; ?> However, I see absolutely no reason to use a database to store only ONE image!? If you are trying to "protect" the image from hot-linking there are much better ways of doing that. And, the method you have does not protect the image from being copied if that is what you are after. Quote Link to comment Share on other sites More sharing options...
Bopo Posted April 1, 2009 Author Share Posted April 1, 2009 Hi Many thanks for the reply, I can understand why it's illogical to store just 1 image in a table, however after trying various pieces of code, I copied your example exactly, sadly no image is displayed, and the img tag remains the same, I'm quite certain that everything is fine with the image in the database, so I'm unsure what to try next. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2009 Share Posted April 1, 2009 Well, I've tested it and it works for me. I inserted a file into the database using a mediumblob type. Then I had two pages: one for the HTML page that would display the content (including the image tag) and a second page that would be used as the SRC for the image tag. content page <html> <body> Image:<br /> <img src="userimage.php"> </body> </html> Image Page (i.e. userimage.php) <?php // Connect to DB $dbh = mysql_connect('localhost', 'root', ''); mysql_select_db('testDB'); $query = "SELECT `image` FROM `userimage`"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) == 1) { $record = mysql_fetch_assoc($query); $img_content = $record['image']; } else { $img_content = readfile('profile_blank_gray.jpg'); } header('Content-type: image/jpeg'); echo $img_content; ?> The problem may be in how you are saving/storing the image in the database. Take a look at this tutorial: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx I can understand why it's illogical to store just 1 image in a table Then why do it. What is your reasoning for wanting to make something simple into something complex? Quote Link to comment 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.