in reply to 'Printing' an Image to a Browser

First thing: warnings and strict of course. You seem to have missed the use CGI; on pasting. There are a bunch of places you could write more idiomatic Perl. use POSIX; is redundant, as is the STDOUT in print STDOUT (unless you've used select to change the default handle..). I propose use CGI::Carp; to have nicer webserver errorlogs. Since you don't check your iwant in the current form, someone might pass something like ?iwant=../../../../../../etc/passwd%00 to have your /etc/passwd printed back to them, or since you're using the two-parameter form of open() without specifically using a "<", they might give you ?iwant=>0 to clobber 0.jpg (or combine it with a directory traversal to clobber other files on your box that your script can write to). So check that the parameter only contains what you expected and specify an explicit opening mode for your file, which I prefer to do using the absolutely unambiguous three-argument form of open(). Btw, writing "$path" is functionaly the same as $path; you're not writing a shell script here..
#!/usr/bin/perl -w use strict; use CGI qw(:standard); use CGI::Carp; my $path = "/path/to/images"; my $imagereq = param('iwant'); $imagereq ||= 0; # defaults to image 0 my $image_path = "$path/$imagereq.jpg"; # check there are digits only in the request and that the file exists $image_path = "$path/404.jpg" unless $imagereq =~ /^\d+$/ and -e $imag +e_path; open(IMAGE, "<", $image_path) or die "ACK! $!"; print "Content-type: image/jpeg\n\n"; binmode STDOUT; $/ = \8192; # read data in 8kbyte chunks rather than from LF to LF # see perldoc perlvar print while <IMAGE>;

It would probably be cleaner to use File::Spec::Functions to do the path concatenations. Of course, if the files are located inside your DocumentRoot, rather than sending the file you could just send a Location: redirect.

HTH :-)

Update Aug 12, 2002 (including minor code fix): silly twit, I am. Of course, if $image_path is "$path/$imagereq.jpg", passing ?iwant=>0 to the script will not clobber anything, and simply lead to the -e test failing harmlessly. I'm surprised noone called me on it. Using the three-argument open() is generally recommended anyway; simply a matter of defensive coding.

Makeshifts last the longest.