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

I'm writing a CGI-Script that's supposed to do something very simple that I've done a few times before: Upload an image and save it on the webserver. Here's what I got:
sub handle_image { my $id = shift or die "Internal Error\n"; my $image = param ('image'); my $filename = ''; if ($image) { $filename = '/images/' . $id . '.jpg'; open (IMAGE, '> ..' . $filename); while (my $bytestoread = read ($image, my $buffer, 102 +4)) { print IMAGE $buffer; } close IMAGE; } elsif (-e '../images/' . $id . '.jpg') { $filename = '/images/' . $id . '.jpg'; } return $filename; }


As you see the routine checks for data to save or, if no data available, for an existing image and returns the filename.

The odd thing is, that this script works fine on my local workstation, but not on the webserver. $image holds the filename of the image, but the filehandle (<$image>) is empty. And yes, I'm using enctype="multipart/form-data" and the directory is writeable for the webserver. It creates a new but empty file.. I've checked all nodes about CGI.pm and file uploading but I couldn't find an answer.
Are there any differences between different versions of CGI.pm or perl that might cause that problem?

Regards,
octopus
--
GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+

Replies are listed 'Best First'.
Re: strange CGI.pm behavior
by chromatic (Archbishop) on Feb 10, 2001 at 23:38 UTC
    I've had trouble with this too. The solution I found was to use the upload() method to get a filehandle:
    my $filename = $query->param('filename'); my $fh = $query->param('file'); # clean $filename, especially in Taint mode open(OUTPUT, '>' . $path . $filename) or carp "Can't open $filename: $ +!"; select(OUTPUT); print while (<$fh>);
    Ugly, demo code, but that technique worked in my tests. See Re: CGI.pm file upload freaking me out for more information.
      Thanks for your advice. I tried to use &CGI::upload and it worked fine on my workstation (again..). But it didn't work on the server. Basically the same thing. The error-message said that the routine doesn't exist. I guess there's something wrong with perl and/or CGI.pm.. I dropped the webmaster a note to check his installation..
      Regards,
      octopus
      --
      GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+