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

Dear monks, it's 12:30am here and I am having a "brain fart". I couldn't work out why the CGI script below does not display the image properly. Basically I want the CGI script to push a jpeg image to the web browser. But when I run it from the browser, the IE6 browser just 'hangs', I can see it's waiting for something. The script works when invoked from command-line though, I can see the content-type header and the image that follows. Can someone please help me to spot what's going wrong in my script? Thanks a lot in advance.
#!E:/Perl/bin/perl.exe -w use strict; use Bytes; use CGI; my $cgi = new CGI; #my $cgi_cust = $cgi->param("cust"); #my $cgi_bill = $cgi->param("bill"); my $cgi_cust = "Telstra"; my $cgi_bill = "telstra-2003-07-28.jpg"; die unless defined $cgi_cust && defined $cgi_bill; my $root = "E:/appdoc/Internet Banking/Bills & Notices"; my $img; # load the image into memory open IMG, "<", "$root/$cgi_cust/$cgi_bill" or die; binmode IMG; seek IMG, 0, 2; my $size = tell IMG; seek IMG, 0, 0; sysread IMG,$img,$size; close IMG; if (defined $img) { binmode STDOUT; print $cgi->header( -type=>'image/jpeg'); print $img; # I think something is doggy here } else { print $cgi->header, "<HTML><BODY><P>IMAGE NOT FOUND</P></BODY></HT +ML>\n"; }

Replies are listed 'Best First'.
Re: Image display problem with CGI script
by PodMaster (Abbot) on Dec 12, 2003 at 14:14 UTC
    sysread IMG, $img, -s IMG;
    `perldoc -f -x'

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Image display problem with CGI script
by blahblahblah (Priest) on Dec 12, 2003 at 14:06 UTC
    I tried your script on my windows 98 with perl 5.6.1 and apache 1.3.12, and IE 6, and it works for me. I modified only the hardcoded paths.

    One tip: instead of just calling die, you ought to print out an error page.

    I tried commenting out the line that you say is "doggy". When I do that, I get a little broken image box with the red x in IE. I couldn't figure out any way to make the script hang.

Re: Image display problem with CGI script
by Paulster2 (Priest) on Dec 12, 2003 at 13:59 UTC

    Not that I know exactly what is going wrong here, but can you try running the script from the command line? I have found that by the output of the script (namely the HTML code that is produced) you can decipher where it is hanging and this will get you on the road to where you need to be.

    I don't know if it will work in your case, because it looks like your running windows, but it's worth a try. Unfortunately I don't have the access between my two machines, this one and the one I can run Perl on, so I can't try it for you.

    I hope that maybe this idea will help you decipher your problem. Other than that, get some sleep, it always helps!!

    Paulster2

      Hi Paulster2, thanks for the comment on getting some sleep. ;-)

      Yes that's exactly what I did soon after I posted the node. After a good night's sleep, I have found out what's wrong with my Apache server in the morning.

      Well, *nothing* was wrong with Apache, it's the Windows XP that Apache runs on that caused the problem. I haven't rebooted the box for a week, the performance of Apache deteriorated gradually, and funny things began to appear.

      I never have to worry about rebooting when I had the Linux box earlier. I have only rebooted once or twice when I relocate. So when I move the server on to a Windows box, I never had the habbit of rebooting the box. Now I begin to wonder if I made the right decision at all to move from Linux to Windows. But it's a bit late now that I have over 160GB of stuff stored on NTFS partitions, so I am thinking about getting a cheap PC and install linux on it, and use that as my web server, while still keeping the XP box as it is.

        The separate *nix box may make sense for you if you have the H/W.   I needed to be portable _and_ have both WinXP and Linux, and so have VMWare installed to run Linux hosted on WinXP.   I can duplicate my target environments (yes, plural) as needed.   It is a very nice setup all in all.

        So, what you are telling me is that not only you needed some sleep, but the box did too??;-)

        Believe me I understand about the Windows thing. I'm a Govie contractor who likes UNIX, but gets stuck working with PC's all the time. It got so bad after one install, I almost threw out my home PC!
        I am also thinking about building a Linux machine for home use. It would work great as a webhost of sorts (I think! Never used the Linux flavor, but love UNIX. I understand it's fairly transparent, within reason.)
        Glad you got your problem fixed! Hope to be seeing you around the PM site.

        Paulster2

Re: Image display problem with CGI script
by injunjoel (Priest) on Dec 12, 2003 at 17:53 UTC
    Greetings all,
    Just a quick question. Why are you opening the file in the first place? Since this is a CGI script I would bet you are viewing this script via a web browser in which case you could simply check for the existence of the file, if its there pass the path(url) to the browser else error out.
    Just a thought since from looking at your code above you are not really doing anything with the image that requires reading it in binarily (is that a word?).
    Of course I have not seen the rest of your code so I could be wrong.
    -injunjoel
      Hi injunjoel, that *is* the rest of the code. The images I want to display is stored under local directories, and I don't want to make the files accessible from the Web, I only want to access them via a CGI script, so I can control the access later. I put in the binary mode because the script was not running properly and I wanted to see if that would make any difference.

Re: Image display problem with CGI script
by talexb (Chancellor) on Dec 12, 2003 at 18:13 UTC

    I think you probably want to do something like this:

    print $cgi->header( -type=>'image/jpeg', -attachment=>$jpegFile); open( JPEG, $jpegFile ) || die "Unable to open $jpegFile for read: $!"; my $buffer; while ( sysread( JPEG, $buffer, 65536 ) ) { print $buffer; } close(JPEG);
    --t. alex
    Life is short: get busy!

    Update: Corion and are disagree about whether a binmode is required .. anyone? Anyone?

    Update 2: Corion pointed out that under Win32, binmode will be required .. but that's on the server, not on the client, since it's on the server that the CGI is being run. I forget that because I've been programming under Linux for the last five years -- binmode isn't required there.

      Hi thanks talexb I think the problem is not with my script, but rather with my apache server, as I have just discovered. I invoked the cgi from another computer (other than the web server), I could see the images being properly displayed. Just that when I tried to access from the server box, the cgi hangs. So I am going to look a bit more into how my apache server was configured, hopefully that could give me more clue.

      I have tried to add the attachement option to my script, and it still not work from the server, but it works from a differnet computer with or without the attachment option.

      Thanks for the comment anyway. ;-)

      Corion and are disagree about whether a binmode is required .. anyone? Anyone?

      Why? binmode can never hurt. Nothing will break if you add binmode when it's not needed, however, everything will break if you don't add it when it is.