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.


In reply to Re: 'Printing' an Image to a Browser by Aristotle
in thread 'Printing' an Image to a Browser by Superlman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.