in reply to Re: Display an image from cgi
in thread Display an image from cgi

roboticus: Thank you for your quick reply. I found out that you have to move the images to a folder outside of the cgi-bin. I put the image folder in the htdocs and I used the following code and it seems to work. It seems cgi-bin blocks images.
print "<td><img src='../images/".$name.".jpg' width='200' height='150' + /></td>";
Thank you for all your help, you gave some good information. Randy

Replies are listed 'Best First'.
Re^3: Display an image from cgi
by kennethk (Abbot) on Oct 19, 2014 at 15:29 UTC
    It seems cgi-bin blocks images.
    Putting a document in the cgi-bin tree is an instruction to your webserver that it is supposed to execute the file, not just serve it up. This leaves two general choices: put your images in another branch of the www tree (e.g. html) or put your images in cgi-bin/images or equivalent, and use a script like the following to serve them up:
    #!/usr/bin/perl -wT use strict; use CGI; my $cgi = CGI->new; my ($id) = $cgi->param('id') =~ /^(\w+)$/; my $filename = "$id.png"; my $content = eval { open my $fh, '<', "images/$filename" or die "Open failure: $!"; binmode $fh; <$fh>; } print $cgi->header(-type => 'image/png', -expires => '+1h', -content_disposition=>"inline; filename=$filename" ); if ($content) { print $content } else { open my $fh, 'images/none.png' or die; binmode $fh; print <$fh>; }

    Note that I've assumed all images are in one directory, and that id's are all word characters (alphanumerics or underscore). It is very easy to make a script that looks a lot like this to will serve up any file on your file system, so pay attention to what paths you let through.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.