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

I am having difficulties doing something very simple I have an image on my server, logo.gif I am trying to print the correct mime type, then open and print the file which always results in a nice little broken image icon. If I do the same thing with excel doc's, html files, yadda yadda it seems to work fine. I know the image file is good because if I open it in my browser from its native souce at domain.com/images/logo.gif it works. I know perl can find the image because is I change the content type to text/html I get all the garbally goo. My code
my $mime = "image/gif";
print "Content-type: $mime\n\n";

open(thefile,"images/logo.gif");
	binmode(thefile); 
	while(<thefile>) {
		print $_;
	}
close(thefile);
Any suggestions?

Replies are listed 'Best First'.
Re: open and print image
by ikegami (Patriarch) on May 30, 2009 at 04:35 UTC

    Did the open succeed? What HTTP status code was returned for the image? (Easier to see if you type in the CGI url directly in the address bar.) What's in the access and error logs?

    As an aside, it makes no sense to use <> on a binary file unless you set a record length using $/ since you might end up reading the file a character at a time or the whole file at once.

    open(my $fh, '<', $qfn) or die($qfn); binmode($fh); local $/ = \( 64*1024 ); print while <$fh>;
Re: open and print image
by Anonymous Monk on May 30, 2009 at 10:43 UTC
    open(FH, '<', 'images/logo.gif') or die $!; local $/ = undef; my $content = <FH>; close(FH); binmode STDOUT; print "Content-type: image/gif\n\n"; print $content; exit(0);
      That worked, thanks! I didn't even think of loading the file to a string/array then printing it out after as opposed to printing it byte by byte. Thanks!