Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a program that inserts (I think, at least the size looks right) an image into a Mysql database. Now I want to take the image from the database and put in on a web page. I know that I can get the the image data with a select command (and it appears to work). But how dows one *use* the data since the img src="yada.png" wants a filename, rather than a file?
  • Comment on Displaying an image stored in MySql for HTML

Replies are listed 'Best First'.
Re: Displaying an image stored in MySql for HTML
by little (Curate) on Jan 24, 2002 at 22:14 UTC
    If you have a nice webserver such as apache for example it will allow you to Alias locations, that means, if you say in your httpd.conf or in an .htacces file:
    Alias /images /cgi-bin/image_handler.cgi
    _all_ requests for http://your.domain.com/images/* on your server will be directed to the named script and the part of the request that looks like a path for a user is passed as query string to your script, and then you have to send the correct MIME type header before starting the data output.
    As nearly about always merlyn already wrote a column about that. :-)
    Have a nice day
    All decision is left to your taste
      Thanks folks. I guess I have some reading to do. This will *finally* get me going.
Re: Displaying an image stored in MySql for HTML
by Anonymous Monk on Jan 24, 2002 at 22:10 UTC
    Write a CGI script that'll spit out the image.
    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; use DBI; my $dbh = DBI->connect("DBI:mysql:database", "user", "password", {Rais +eError => 1}); my $sth = $dbh->prepare("SELECT img FROM table WHERE id = ?"); $sth->execute(param('id')); my $img = $sth->fetchrow_arrayref; print header("image/jpeg"), $img->[0];
Re: Displaying an image stored in MySql for HTML
by larryk (Friar) on Jan 24, 2002 at 22:02 UTC
    Write the data into a temporary file and use that. Try File::Temp
       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"