[ back to toc ]


Date: 2002/06/06 12:07

I've a simple Perl script that can display image
when it's resides in DocumentRoot directory:
(/var/www/dev/htdocs) but it can't display image
in the cgi-bin directory(/var/www/cgi-bin). The script for image test is
listed as follows:
use CGI qw(:standard);

print header('image/gif');
print "<img src=\"array.gif\">";
print "<p>Image test";

I'm very confused about it since I've already
changed the content type of header.

Thank you for your help!

>I'm very confused

Yes, you are, but I will help you now.

The script that you listed sends an HTML page to the browser. This HTML
page contains a reference to a picture and therefore the browser starts a
new http request for the picture. The Web server, this time without
starting any CGI program, sends the content of the picture to the client.

The URL in your case is relative to the script. This printed

print "<img src=\"array.gif\">";

means that the picture "array.gif" should be in the same directory as the
CGI script. This is fine when the picture along with the CGI script is in
the directory /var/www/dev/htdocs. However when you put the script along
with the picture into the directory /var/www/cgi-bin problem arises. The
script starts and downloads the HTML page. The browser sees that there is
a picture reference in the HTML page and starts an HTTP request. This time
the URL refers to a file, which is in the directory /var/www/cgi-bin. The
web server knows from its configuration that all request that refers this
directory has to be served starting the file as a CGI program. So it tries
to start the GIF file as a program. The UNIX system can not exetute a GIF
file and thus the Apache web server sends a HTTP answer with error code.
This is recognized by the browser but can not display it textually,
because the place to put it is a picture. Therefore the browser displays a
broken picture.

BTW: The line

print header('image/gif');

is erroneous, because your script sends the HTML file with reference to
the GIF and not the GIF. Thus the mime type of what you send is

print header('text/html');

You can also:

use CGI qw(:standard);

print header('image/gif');
undef $/;
binmode F;# just in case it runs under Win32
$q =<F>;
close F;
binmode STDOUT; # again Win32
print $q;

to send the picture itself.


[ back to toc ]