Jump to content

How to echo back images


j05hr

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.