Re: Frustrated, puzzled and confused: outputting images from a database
| From: | Jon Rosenberg | Date: | Tue, 12 Dec 2000 23:25:15 +0000 |
| Subject: | Re: Frustrated, puzzled and confused: outputting images from a database | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29981@lists.php.net to get a copy of this message | ||
Here's what I did.
for PHP, I have only Magic-Quotes-gpc ON, the other two are off.
The following takes image from form and uploads to teh DB:
$temp_pic = fopen($form_data, 'r');
$temp_pic2 = fread($temp_pic, filesize($form_data));
$bin_pic1 = addslashes($temp_pic2);
$bin_pic = addslashes($bin_pic1);
$add_to_db = add_to_db( $_fname, $_lname, $_email, $_uname, $_pword,
$_sex, $_age, $_city, $_state, $_orientation, $_status, $_race, $_religion,
$_height, $_weight, $_hair, $_eyes, $_education, $_occupation, $_interests,
$_match, $_other, $_date, $bin_pic, $form_data_name, $form_data_type,
$dberror );
to pull from the database, I do this:
if ( ! $pic == "" )
{
$bgcolor4 = "gray"; //table background color
print "<td><table border=\"0\" cellpadding=\"5\"
bgcolor=\"$bgcolor4\">\n\n<tr>\n<td
bgcolor=\"$bgcolor2\"><center>Picture</center></td>\n</tr>\n<tr>\n<td
bgcolor=\"$bgcolor\">";
print "<a href=\"" . $home_url . "pullimg.php?uname=$_uname\"
target=\"_blank\"><img src=\"pullimg.php?uname=$_uname\"
width=\"150\"><br
/>Click to see full size</a></td>\n\n";
print "</tr>\n</table></td>\n";
}
And finally, here is the source of pullimg.php
if ( ! mysql_select_db( $db, $db_connect) )
{
$dberror = mysql_error();
return false;
}
$search_result = mysql_query( "SELECT bin_pic, filename, filetype FROM
$db_table WHERE _uname = '".$uname."'", $db_connect );
$num_rows_returned = mysql_num_rows( $search_result );
if ( $num_rows_returned == 1 )
{
$results_returned = mysql_fetch_array( $search_result );
$pic_data = $results_returned['bin_pic'];
$pic_name = $results_returned['filename'];
$pic_type = $results_returned['filetype'];
}
else
{
print "There has been an error!<br />";
exit;
}
Header("Content/type: $pic_type");
$pic_content = stripslashes($pic_data);
echo($pic_content);
mysql_close( $db_connect );
hope that helps!
Jon
----- Original Message -----
From: "Jason Murray" <Jason.Murray@melbourneit.com.au>
To: <php-general@lists.php.net>
Sent: Tuesday, December 12, 2000 6:20 PM
Subject: [PHP] Frustrated, puzzled and confused: outputting images from a
database
> Hi folks,
>
> I'm storing image data (either image/gif or image/[p]jpeg) (and its
> mimetype) in a BLOB column of a MySQL database.
>
> I have written a PHP script to pull the image data and mime data for
> a particular ID in the database, and output. This does seem to be
> working (touch wood).
>
> Before someone suggests writing the file out and storing the filename,
> this won't work. We're on load-balanced servers without any file sharing
> means between them.
>
> My problem is that when I pull the image data from the database, I'm
> not sure what to do in order to output it. I've tried:
>
> <?
> Header("Content-Type: image/gif");
> echo $imagedata;
> Exit();
> ?>
>
> But this only seems to produce a broken image in a browser, and when
> I look at it on a command line it *seems* to work. Please excuse crap
> below:
>
> [jason@phpdev htdocs]# curl
>
> "http://<<hostname>>/dbgfx.php3?affcode=21&logotype=new"
> ...$OEr°IE(4'ÂBY$ÿ¼"Hç
> É
> ')¤oere/L(_-T)³WK 7o?q'Ë Á^HEUR0°L îà
> %vñ¢
hh"f-°÷E...f~!?r¸!...¢cÚ:~Xâ?¨¢?,v(¡¾ý"Ø{4
> Å8
> 8ÔB|]ô-8ü~ÙyEUR5-KìÐUOE`}r?<=D)å"TVÉCe
> áá È\·ÌÉø'Ç×§'YY&Ùy÷]\Õ11
>
> oef`Hbh>Ó&oeOEÙ5-'Y¦oe^
>
> ¨ádã8R1ã
> BV#]ÆÒ"1uÈQ0,3¥S~6*ÁÊX
> SB
> LHc'GåËh½
> ¤»Fé~^10 fTh´²×0e
> 76"}
> pGPÍÿz
> Rúò-¨À-±s8?áZ7...ÑÄ{vÉË1[jason@phpdev htdocs]#
>
>
> Do I need to use image functions? I thought I would be able to get by
> without them. Can I just echo the binary data to the browser?
>
> I'm completely puzzled. This *should* be working. Please help...
>
> Jason
>
> --
> Jason Murray
> jasonm@melbourneit.com.au
> Web Design Team, Melbourne IT
> Fetch the comfy chair!
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>