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

Hello Monks,

I am new to Perl and CGI program.

I would like to display an image using CGI program.

My code is as below

# cat image

#!/usr/bin/perl

use File::Copy;

print "Content-type: image/jpeg\n\n";

copy "/var/www/cgi-bin/PTF//WP_20140410_19_01_49_Pro.jpg", \*STDOUT;

#

I am getting below error in httpd logs

 [Tue Apr 29 14:15:09 2014] [error] [client 10.142.236.15] malformed header from script. Bad header=\xff\xd8\xff\xe1\xa7\xdcExif: image1

Please help me to resolve this issue

Replies are listed 'Best First'.
Re: Not able to display an image in perl CGI
by Corion (Patriarch) on Apr 29, 2014 at 09:39 UTC

    Do you need to output an HTTP status line?

    print "HTTP/0.9 200 OK\r\n"; print "Content-Type: image/jpeg\r\n"; print "\r\n"; ...
      # cat image1 #!/usr/bin/perl use File::Copy; print "HTTP/0.9 200 OK\r\n"; print "Content-Type: image/jpeg\r\n"; print "\r\n"; copy "/var/www/cgi-bin/PTF//WP_20140410_19_01_49_Pro.jpg", \*STDOUT; #

      After introducing Header information also i am getting the same error..

      [Tue Apr 29 14:52:59 2014] [error] [client 10.142.236.15] malformed he +ader from script. Bad header=\xff\xd8\xff\xe1\xa7\xdcExif: image1
        What webserver? Try
        #!/usr/bin/perl -- use File::Copy; use CGI qw( header ); my $file = "/var/www/cgi-bin/PTF//WP_20140410_19_01_49_Pro.jpg"; print header( -type => q{image/jpeg} , -nph => 1, -Content_length => - +s $file ); copy $file, \*STDOUT;

        FWIW, its looking more and more like your webserver configuration is the problem