in reply to Display an image from cgi

rshoe:

You're not giving the complete URL for the image. You'll have to tell the browser the URL so it can ask for the image from the server in such a way that the server can find it. You probably don't want to give the actual path to the file, since web servers normally use a different directory as a root location.

Suppose your file is at /My/Stuff/www/images/tools/hammer.jpg and your server name is "www.MyTools.com", and it's treating /My/Stuff/www as the document root. Then you'll want to set the URL to "http://www.MyTools.com/images/tools/hammer.jpg".

Note: There may be more subtleties that I'm not aware of, as I don't write code for the WWW .... yet! (My new job is wanting me to do some, so I've been studying a bit.)

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Display an image from cgi
by rshoe (Novice) on Oct 18, 2014 at 22:59 UTC
    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
      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.