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

I'm stumped. I have a script that uploads a file to the server with an associated description on a linux system running Apache and Perl 5.6. This script works on other similar systems, but not on this one. The form looks like this:

<FORM ENCTYPE="multipart/form-data" ACTION="upload.cgi" METHOD=POST> Upload this file: <INPUT NAME="userfile" TYPE="file"><br> Description:<input type="text" name="description"><br> <INPUT TYPE="submit" VALUE="Send File"> </FORM>

The code that's processing it looks like this:

$rawfile = $cgi->param("userfile"); if ( $rawimage =~ /([\w\s\W]+)\.(\w+)/ ) { $ext = $2; } $filename = time() .".$ext"; open (OUTFILE,">./upload/$filename") or die "Could not upload file $ +filename: $!"; while ($bytesread=read($rawimage,$buffer,$ENV{'CONTENT_LENGTH'})) { print OUTFILE $buffer or die "Could not upload file $filename: $!" +; } close (OUTFILE);

And it doesn't work. It creates a zero byte file in the correct directory. I've also tried processing it like this:

use CGI qw(:all escape); use CGI::Carp qw(fatalsToBrowser); $cgi = new CGI; read($cgi->param("userfile"), $buffer, $ENV{'CONTENT_LENGTH'}); open (x,">$tempfile"); print x $buffer; close (x);

But if I take out the description field from the form, and replace $cgi->param with STDIN, it works! I've tried using $cgi->upload, to no avail.

The really bizarre thing is that if I copy the script from one account to another account with the same ISP, the original code works! I'm so baffled by this, I'm resorting to using exclamation points! Any help in figuring out why $cgi is not passing the file on would be appreciated! Thanks!

Replies are listed 'Best First'.
Re: file from multipart form not being written to the server!
by antirice (Priest) on Jun 22, 2003 at 05:21 UTC

    Just wondering, but where is $rawimage set? If it isn't and you meant to use $rawfile (which I think is the case), then you may wish to use strict;. Of course, this causes the read to issue warnings and whatnot which is why upload is now available. Hope this helps. If not, that's the only problem that I see. (without going into considerations for security and whatnot...i.e. what's stopping someone from eating the hard drive alive?)

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Actually, that was a typo. In the code it is actually $rawfile.

      I do have use strict set. It's just that there is a lot more script, and I was paraphrasing the snippet to make it more legible.

Re: file from multipart form not being written to the server!
by gellyfish (Monsignor) on Jun 22, 2003 at 09:33 UTC

    I think antirice is correct. This would have been caught if you been using strict, but with strict in place you are also unable to use the filename returned by param() as a filehandle. You probably want to consider using the upload() method instead:

    #!/usr/bin/perl -w use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use File::Basename; my $fh = upload('userfile'); my $filename = param('userfile'); my ( undef, undef, $ext ) = fileparse($filename, '\.\w+$'); $filename = time() . $ext; open OUTFILE, "> /tmp/$filename" or die "/tmp/$filename - $!\n"; binmode OUTFILE; binmode $fh; print header('text/html'), start_html(), p("filename : $filename (saved in /tmp)"); while (<$fh>) { print OUTFILE $_; } print end_html();
    /J\
    
      Thanks. I'll give this a go. Aren't you supposed to close OUTFILE though?