FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Re: Displaying a longblob as an image
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Displaying a longblob as an image [message #170250] Fri, 22 October 2010 20:30 Go to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Robert Tomsick wrote:
> Jerry Stuckle wrote:
>
>> On 10/22/2010 1:36 PM, Robert Tomsick wrote:
>>> The Natural Philosopher wrote:
>>>
>>>> anu wrote:
>>>> > I am storing an image as a longblob in MYSQL. I would like to know how
>>>> > I can display the image in my form in PHP since the image is stored as
>>>> > ascii in the MySQL table.
>>>> If its a longblob, it wont be in ASCII...
>>>>
>>>> You display in two stages
>>>>
>>>> You need an IMG statement that references a php program that can send
>>>> the image.
>>>>
>>>> e.g. I have this..
>>>>
>>>> printf("<IMG border=\"0\" src=\"send_picture.php?id=%d\" alt=\"%s\">",
>>>> $id,(mysql_result($result,$i,'picture_filename')=="")?"
>>>> No Image":$name);
>>>>
>>>> where the picture is stored in a table (product) blob along with its
>>>> name.
>>>>
>>>> The actual record ID is passes to send_picture.php.
>>>> This is the code in that script.
>>>>
>>>> The file lib.php contains general authentication and database code.
>>>>
>>>> The mimelib.php is really just to have the function get_mime, which in
>>>> my case simply reads the mime type from the linux list of mime types.
>>>>
>>>>
>>>> send_picture.php
>>>> ================
>>>>
>>>> <?php
>>>> include('lib.php');
>>>> include('mimelib.php');
>>>> open_database(); // ready to check
>>>> $id=$_GET['id'];
>>>> $query="select picture, picture_filename, picture_size from product
>>>> where id='".$id."'";
>>>> //echo $query;
>>>> $result=mysql_query($query);
>>>> if(($result>0)&& (($rows=mysql_numrows($result)) == 1)) //got some data
>>>> {
>>>> $name=mysql_result($result,0,'picture_filename');
>>>> $content=mysql_result($result,0,'picture');
>>>> $size=mysql_result($result,0,'picture_size');
>>>> }
>>>> else die();
>>>> if ($name="") die();
>>>> $mtype=get_mime($name);
>>>> header("Content-Type: ".$mtype);
>>>> header("Content-Disposition: inline; filename=\"".$name."\"");
>>>> header("Content-Transfer-Encoding: binary");
>>>> header("Content-Length: ".strlen($content));
>>>>
>>>> print $content; ?>
>>>>
>>>>
>>>> ** NOTE. Leave nothing at all after the closing ?> or it gets added to
>>>> the download. **
>>>>
>>>>
>>>> mimelib.php
>>>> ===========
>>>> ?php
>>>> // looks up mime type in /etc/mime.types and returns the type, or a
>>>> default if unmatched
>>>> // makes no attempt to interrogate the file content as such.
>>>> // THIS NEEDS MORE WORK!!! it doesn't get all types..espcially DWG/DXF!!
>>>> // Mind you we don't want to inmvoke plug-ins for these..
>>>> function get_mime($filename)
>>>> {
>>>> $default="application/force-download";
>>>> // first extract the extension
>>>> $array=explode(".",$filename); // split the name into the bits separated
>>>> by periods
>>>> $count=count($array);
>>>> if ($count<2) // if there IS NO extension..
>>>> return $default; // and let the user sort it out.
>>>> $ext=$array[$count-1]; // it will be the last element in the array..
>>>> $fp=fopen("/etc/mime.types", "r");
>>>> if(!$fp) return ($default); // no /etc/mime.types file
>>>> while (!feof($fp))
>>>> {
>>>> $buffer = fgets($fp, 128);
>>>> if (ctype_space($buffer{0}) || $buffer{0}=='#' ||
>>>> $buffer{0}=='\n')
>>>> continue; // skip empty lines. or lines starting with spaces
>>>> or hashes
>>>> sscanf($buffer, "%s %s %s %s %s %s \n",$mime_type,$extension,
>>>> $extension1, $extension2, $extension3, $extension4);
>>>> if ($ext==$extension || $ext==$extension1 || $ext==$extension2 ||
>>>> $ext==$extension3 || $ext==$extension4 )
>>>> {
>>>> fclose ($fp);
>>>> return($mime_type);
>>>> }
>>>> }
>>>> fclose($fp);
>>>> return $default;
>>>> }
>>>> ?>
>>> Needless to say, you shouldn't follow the above code exactly, as doing so
>>> will make it trivial for some jerk to come along and nuke your
>>> database...
>>>
>> He shouldn't follow it at all, since it doesn't do the job he needs.
>> Typical TNP response, though.
>
> TBH, I didn't really check to make sure it was an appropriate solution. The
> query string was what caught my eye.
>
> Ah well...
Well it does the job for me (displays a blob image onscreen), but the
stucklehead knows very little about real world programming.

He's been out of work for years now.
Re: Displaying a longblob as an image [message #170251 is a reply to message #170250] Fri, 22 October 2010 20:45 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(The Natural Philosopher)

> Well it does the job for me (displays a blob image onscreen), but the
> stucklehead knows very little about real world programming.

This "real world programming" is what regularly appears on TheDailyWTF
and in security bulletins.

Your posted code is not only insecure, but obviously from the last
century. There's a lot of deprecated stuff in it and many things to
optimize. In one word: ugly.

Micha
Re: Displaying a longblob as an image [message #170252 is a reply to message #170251] Fri, 22 October 2010 20:48 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Michael Fesser wrote:
> .oO(The Natural Philosopher)
>
>> Well it does the job for me (displays a blob image onscreen), but the
>> stucklehead knows very little about real world programming.
>
> This "real world programming" is what regularly appears on TheDailyWTF
> and in security bulletins.
>
> Your posted code is not only insecure, but obviously from the last
> century. There's a lot of deprecated stuff in it and many things to
> optimize. In one word: ugly.
>
> Micha
Nevertheless it shows the principle

I am indeed from the last century.

I note no one else HAS posted a solution.

Those that can, do.

Those that can't teach.

Those that cant teach, sit and criticise.
Re: Displaying a longblob as an image [message #170253 is a reply to message #170251] Fri, 22 October 2010 21:27 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 10/22/2010 4:45 PM, Michael Fesser wrote:
> .oO(The Natural Philosopher)
>
>> Well it does the job for me (displays a blob image onscreen), but the
>> stucklehead knows very little about real world programming.
>
> This "real world programming" is what regularly appears on TheDailyWTF
> and in security bulletins.
>
> Your posted code is not only insecure, but obviously from the last
> century. There's a lot of deprecated stuff in it and many things to
> optimize. In one word: ugly.
>
> Micha

Micha, not surprising. It's just something he copied from a web site
but doesn't understand.

Examine the code and it doesn't even display the image from the database
like the op wants.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Displaying a longblob as an image [message #170256 is a reply to message #170252] Sat, 23 October 2010 02:04 Go to previous message
Magno is currently offline  Magno
Messages: 49
Registered: October 2010
Karma: 0
Member
On 10/22/2010 05:48 PM, The Natural Philosopher wrote:
> Michael Fesser wrote:
>> .oO(The Natural Philosopher)
>>
>>> Well it does the job for me (displays a blob image onscreen), but the
>>> stucklehead knows very little about real world programming.
>>
>> This "real world programming" is what regularly appears on TheDailyWTF
>> and in security bulletins.
>>
>> Your posted code is not only insecure, but obviously from the last
>> century. There's a lot of deprecated stuff in it and many things to
>> optimize. In one word: ugly.
>>
>> Micha
> Nevertheless it shows the principle
>
> I am indeed from the last century.
>
> I note no one else HAS posted a solution.
>
> Those that can, do.
>
> Those that can't teach.
>
> Those that cant teach, sit and criticise.

Don’t reply to the stupid and ignore them. They only seek for attention
by attacking others. Plonk them.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Displaying a longblob as an image
Next Topic: US, Canada or International
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 19:25:32 GMT 2024

Total time taken to generate the page: 0.02835 seconds