j05hr Posted May 19, 2010 Share Posted May 19, 2010 I am manually uploading an image to the database as a blod and when you go to echo it back, it comes out with a load of binary looking stuff. This is my code in the page. How can I make the actual image come out? <div class="picture"><?php echo strip_tags(nl2br($sel_page['photo']), "<b><br><span><p><a>"); ?></a></div> Thanks, Josh Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2010 Share Posted May 19, 2010 Every image on a web page requires a HTML <img src="url_of_the_image" alt=""> tag. The url_of_the_image must be to a .php script that outputs the correct content type header followed by the image data. Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060503 Share on other sites More sharing options...
j05hr Posted May 19, 2010 Author Share Posted May 19, 2010 So in my line of code do I put... <div class="picture"><img src="/<?php echo strip_tags(nl2br($sel_page['photo']), "<b><br><span><p><a>"); ?>"/</a></div> Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060505 Share on other sites More sharing options...
jamesxg1 Posted May 19, 2010 Share Posted May 19, 2010 You want something like this. photo.php <?php session_start(); $get = "SELECT `image` FROM `images` LIMIT 1"; $run = mysql_query($get); if($run && mysql_num_rows($run) == 1) { header("Content-type:image/jpeg"); while($images = mysql_fetch_assoc($run)) { readfile('PATH_TO_IMAGES/' . $images['image']); } } ?> Then use this. <img src="photo.php"> James Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060520 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 No, you put: <div class="picture"><img src="photo.php?id=.."></div> And in photo.php you decide on the appropriate header and echo out the data, something like: header('Content-Type: ' . $row['mime']); echo $row['photo']; Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060524 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 @James session_start()? header("Content-type:image/jpeg");? What if the content came from a GIF? Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060525 Share on other sites More sharing options...
jamesxg1 Posted May 19, 2010 Share Posted May 19, 2010 The only reason he would need a get param is if he is selecting the image with a WHERE! But if you want to go that deep into the headers then here. <?php $id = mysql_real_escape_string(trim($_GET['id'])); $get = "SELECT `image` FROM `images` WHERE `id` = '$id' LIMIT 1"; $run = mysql_query($get); if($run && mysql_num_rows($run) == 1) { while($images = mysql_fetch_assoc($run)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); header("Content-type:" . finfo_file($finfo, $images['image']) . ""); readfile('PATH_TO_IMAGES/' . $images['image']); finfo_close($finfo); } } ?> James. Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060527 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 I think it's quite obvious that he wants a specific image to show up (therefor the ID parameter) instead of only the first one always? Why use the LIMIT-clause, if ID is a PK it will only be present once as the PK is a unique field. And why use while (..) if you just LIMIT-ed to 1 and evaluated mysql_num_rows($run) == 1? $image = mysql_fetch_assoc($run); header('Content-type:image/' . mime_content_type($image['image'])); readfile('PATH_TO_IMAGES/' . $image['image']); Would suffice. Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060529 Share on other sites More sharing options...
jamesxg1 Posted May 19, 2010 Share Posted May 19, 2010 <?php $id = mysql_real_escape_string(trim($_GET['id'])); $get = "SELECT `image` FROM `images` WHERE `id` = '$id' LIMIT 1"; $run = mysql_query($get); if($run && mysql_num_rows($run) == 1) { while($images = mysql_fetch_assoc($run)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); header("Content-type:" . finfo_file($finfo, $images['image']) . ""); readfile('PATH_TO_IMAGES/' . $images['image']); finfo_close($finfo); } } ?> It works, and its pretty good so I find it ok! James Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060531 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 It works, and its pretty good so I find it ok! A real programmer pursues perfection. Don't settle for something that just works, make it better, easier to maintain/extend. And remove any redundant code. Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060561 Share on other sites More sharing options...
j05hr Posted May 19, 2010 Author Share Posted May 19, 2010 I'm just doing this in my local host with an image that is already loaded to my database the id for the image is 1. This is what I'm doing (I know it's wrong but I want to know how to make it right) test.php <html> <head> </head> <body> <div class="picture"><img src="photo.php?id=1"></div> </body> </html> [code] photo.php [code] <?php header('Content-Type: ' . $row['mime']); echo $row['images']; <? Thanks, Josh Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060611 Share on other sites More sharing options...
j05hr Posted May 19, 2010 Author Share Posted May 19, 2010 <div class="picture"><img src="photo.php?id=.."></div> How do I get this code to get the Id of the actual image without typing it in manually? Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060622 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 while ($row = mysql_fetch_assoc($result)) { echo '<div class="picture"><img src="photo.php?id=', $row['id'], '" title="', $row['title'],'" alt="', $row['title'],'"></div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060641 Share on other sites More sharing options...
j05hr Posted May 19, 2010 Author Share Posted May 19, 2010 I also have this code that now shows the picture from the database, but like you said, if it's not a jpg how will it upload? <?php $username = "root"; $password = ""; $host = "localhost"; $database = "images"; @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); @mysql_select_db($database) or die("Can not select the database: ".mysql_error()); $id = $_GET['id']; if(!isset($id) || empty($id)){ die("Please select your image!"); }else{ $query = mysql_query("SELECT * FROM images WHERE id='".$id."'"); $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; } ?> So what I did was, take out their header and put yours in but then the page doesn't load and asks me what program I want to open the code with? Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060653 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 When you upload an image you should also store it's content-type. Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060659 Share on other sites More sharing options...
j05hr Posted May 19, 2010 Author Share Posted May 19, 2010 I am now trying to do the upload part of it, I gues this error message saying, Subject creation failed. Unknown column 'cesc.jpg' in 'field list' This is my code <?php require_once("includes/session.php"); ?> <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php confirm_logged_in(); ?> <?php find_selected_page(); ?> <?php include("includes/header.php"); ?> <div id="nav"> <?php echo navigation($sel_subject, $sel_page); ?><a href="content.php" "onclick="return confirm('Warning: Anything you have written will not save if you click ok');">Back to Menu</a> </div> <div id="banner"> </div> <div id="line"> </div> <div id="content"> <h3>Add Subject</h3> <form action="create_subject.php" method="post" class="newContent"> <p>Subject name: <input type="text" name="menu_name" value="" id="menu_name" /> </p> <p>Position: <select name="position"> <?php $subject_set = get_all_subjects(); $subject_count = mysql_num_rows($subject_set); // $subject_count + 1 b/c we are adding a subject for($count=1; $count <= $subject_count+1; $count++) { echo "<option value=\"{$count}\">{$count}</option>"; } ?> </select> </p> <p>Visible: <input type="radio" name="visible" value="0" /> No <input type="radio" name="visible" value="1" /> Yes </p> <p> <p>Content:<br /> <textarea name="content" rows="10" cols="75"></textarea> </p> </p> <br /> <input name="image" accept="image/jpeg" type="file"> <br /> <br /> <input type="submit" value="Add Subject" /> <br /> <br /> <a href="content.php">Cancel</a> </form> </div> <?php require("includes/footer.php"); ?> All I did was add image where appropriate and put image in my database but it says the image is an unkown column? Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060667 Share on other sites More sharing options...
gwolgamott Posted May 19, 2010 Share Posted May 19, 2010 When you upload an image you should also store it's content-type. To add to this, because ignace is correct on the argument, but you should store the mime type then deal with it that way. This is pretty much already what ignace has instructed you on already but here's a decent tutorial on the subject just FYI for you. http://cookbooks.adobe.com/post_Display_an_image_stored_in_a_database__PHP_-16637.html Quote Link to comment https://forums.phpfreaks.com/topic/202246-how-to-echo-back-images/#findComment-1060709 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.