For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > January 2005 > RE: [PHP-DB] storing images in database









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RE: [PHP-DB] storing images in database
Bastien Koert

2005-01-25, 3:56 pm

you can not ouput html and binary data at the same time. you need to have
image headers sent to the browser to show the image properly.

the best way to do this is to move the image processing code to a separate
page and include it like this

echo '<img src="./path/to/image.php?id=$id">';

then the image page looks like this:
<?php

if($_GET['id']) {
$id = $_GET['id'];
// you may have to modify login information for your database server:
@MYSQL_CONNECT("localhost","root","password");

@mysql_select_db("binary_data");

$query = "select bin_data,filetype from binary_data where id=$id";
$result = @MYSQL_QUERY($query);

$data = @MYSQL_RESULT($result,0,"bin_data");
$type = @MYSQL_RESULT($result,0,"filetype");

Header( "Content-type: $type");
echo $data;

};
?>

bastien



>From: Chip Wiegand <chip.wiegand@simrad.com>
>To: "PHP DB" <php-db@lists.php.net>
>Subject: [PHP-DB] storing images in database
>Date: Tue, 25 Jan 2005 09:11:07 -0800
>
>I have stored a .jpg image in a database, then when I make a sql statement
>to display that image on a web page all I get is the cryptic code in place
>of the image. I am storing it in a row configured as a blob, mime type
>image/jpeg and binary (using phpMyAdmin). What am I doing wrong?
>Regards,
>Chip
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>

Chip Wiegand

2005-01-25, 8:56 pm

Thanks Bastien,
In testing this I have added the code samples to a page and have it
working except the path statement is not correct. For now, I've just added
all the code to one page, rather than including a second page. The
statement - echo '<img src="id=$id">'; is resulting in this error - The
requested URL /id=$id was not found on this server. Any suggestions?
Thanks,
Chip

"Bastien Koert" <bastien_k@hotmail.com> wrote on 01/25/2005 09:45:39 AM:

> the best way to do this is to move the image processing code to a

separate
> page and include it like this
>
> echo '<img src="./path/to/image.php?id=$id">';
>
> then the image page looks like this:
> <?php
>
> if($_GET['id']) {
> $id = $_GET['id'];
> // you may have to modify login information for your database server:
> @MYSQL_CONNECT("localhost","root","password");
>
> @mysql_select_db("binary_data");
>
> $query = "select bin_data,filetype from binary_data where id=$id";
> $result = @MYSQL_QUERY($query);
>
> $data = @MYSQL_RESULT($result,0,"bin_data");
> $type = @MYSQL_RESULT($result,0,"filetype");
>
> Header( "Content-type: $type");
> echo $data;
>
> };
> ?>
>
> bastien
>
>
>
statement[color=darkred]
place[color=darkred]
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Bastien Koert

2005-01-25, 8:56 pm

Its not src='id=$id'> that will defnintely blow up....

echo '<img src="./path/to/image.php?id=$id">';

where $id is the id of the record you are trying to get the image to...

Bastien


>From: Chip Wiegand <chip.wiegand@simrad.com>
>To: bastien_k@hotmail.com
>CC: php-db@lists.php.net
>Subject: RE: [PHP-DB] storing images in database
>Date: Tue, 25 Jan 2005 12:37:15 -0800
>
>Thanks Bastien,
>In testing this I have added the code samples to a page and have it
>working except the path statement is not correct. For now, I've just added
>all the code to one page, rather than including a second page. The
>statement - echo '<img src="id=$id">'; is resulting in this error - The
>requested URL /id=$id was not found on this server. Any suggestions?
>Thanks,
>Chip
>
>"Bastien Koert" <bastien_k@hotmail.com> wrote on 01/25/2005 09:45:39 AM:
>
>separate
>statement
>place
>

Martin Norland

2005-01-25, 8:56 pm

Bastien Koert wrote:[color=darkred]
> Its not src='id=$id'> that will defnintely blow up....
>
> echo '<img src="./path/to/image.php?id=$id">';
>
> where $id is the id of the record you are trying to get the image to...
>
> Bastien
>
[snip][color=darkred]

<?php
function shed_light() {
$wont = "that";
$output = "would";
$variables = "work";
$values = "fine";
$because = "actually";
$of = "without";
$single = "single";
$quotes = "quotes"; // and with a "?" because it's the same script.

echo '$wont $output $variables $values $because $of $single $quotes!';

// echo '<img src="./path/to/image.php?id='.$id.'">';
// echo '<img src="?id='.$id.'">';
}
shed_light();
?>

Cheers,
--
- Martin Norland, Database / Web Developer, International Outreach x3257
The opinion(s) contained within this email do not necessarily represent
those of St. Jude Children's Research Hospital.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com