What you have is on the right track, but you might want to take
a few other precautions:
#!/usr/bin/perl -w
use strict;
use CGI::Carp;
use CGI;
my ($cgi) = new CGI();
my ($gif_name) = 'whatever.gif'; # Or $cgi->parameter('show')
my ($image);
open (GIF, $gif_name) || die "Could not open $gif_name: $!\n";
binmode (GIF);
read (GIF, $image, -s $gif_name);
close (GIF);
binmode (STDOUT);
print $cgi->header("image/gif");
print $image;
Note the use of '-s' to find the complete size of the file,
which allows you to read it in one go.
CGI::Carp will allow you to record any run-time
errors to the appropriate error_log file, especially those
messages generated when you 'die', which normally go sight-unseen.
Update: Added 'binmode(GIF)' as pointed out by
dws below,
and
merlyn as well (Thanks!) No sense in posting 'bad' code.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.