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

I'm working on a CGi page were I print out data from a txt file. The page must print out all the data an even an image associated with file. The scalar $name must produce an image instead of text. I have placed images in the cgi-bin with names that correlate with the variable data $name. When the page is displayed all the information shows but the image is broken. I view the source in the browser and it shows the image to be there but the image is not showing. If anyone can help me I would appreciate it. Thank You, Randy Here is my code from the txt file:
BHW1234|Drill|Black and Decker cordless drill with soft case|$79.97 BHW222|Hammer|Stanley 16-Ounce AntiVibe curved claw steel hammer|$22.4 +9 BHW2054|Handsaw|Dewalt 15 in. Hand Saw with blade armor features Tough +Coat|$18.97 BHW7048|Level|True Blue Empire level. Accuracy, durability and readabi +lity with a Lifetime Warranty. |$49.97 BHW6034|Screwdriver|Klein #2 phillips screwdriver with electrical insu +lstion.|$18.29 BHW9856|Tablesaw|Saw Stop all-purpose versatility, reliability and saf +e.|$1500.00
Here is my code from the cgi file:
#!c:\Dwimperl\perl\bin\perl.exe use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; print "Content-type: text/html\n\n"; print "<html><body>"; print "<div align='center'>"; print "<h2>Bob's Hardware Tools</h2>"; print "<h3>Current Hardware</h2>"; open (HARDWARE, "inventory.txt") or die "Error opening file, $!"; print "<table border='2' cellpadding='5'>"; while(my $hardware = <HARDWARE>){ (my $sku,my $name,my $desc,my $price) = split/\|/,$hardware; print "<tr>"; print "<td><img src='$name.jpg' width='200' height='150' /></td>" +; print "<td>$sku</td>"; print "<td>$desc</td>"; print "<td>$price</td>"; } close HARDWARE; print "</table></div>"; print "</body></html>";

Replies are listed 'Best First'.
Re: Display an image from cgi
by roboticus (Chancellor) on Oct 18, 2014 at 22:10 UTC

    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.

      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.