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

My Perl knowledge is very basic. Below is a very simple code snippet where I upload a file selected by the enduser via an HTML form. The $filename is created in my server's directory as pointed to by $upload. However it is always empty, 0 size. I'm assuming since the name is created properly, in the proper directory, it's not a permission problem. Any thoughts?
#!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; $CGI::POST_MAX = 1024 * 5000; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "/var/www/example.com/htdocs/upload"; my $q = new CGI; my $filename = $q->param("filename"); my $cat = $q->param("cat"); if ( !$filename ) { saysOutput("No file selected"); exit; } my ( $name, $path, $extension ) = fileparse ( $filename, '..*' ); $filename = $name . $extension; $filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g; if ( $filename =~ /^([$safe_filename_characters]+)$/ ) { $filename = $1; } else { saysOutput("yikes"); exit; } my $out = ""; my $upload_filehandle = $q->upload("filename"); open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; binmode UPLOADFILE; my $stat = "."; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; # My function which handles all printing to the web browser saysOutput($stat);
  • Comment on Why am I uploading nothing? $upload_filehandle appears to read nothing
  • Download Code

Replies are listed 'Best First'.
Re: Why am I uploading nothing? $upload_filehandle appears to read nothing
by choroba (Cardinal) on Feb 12, 2022 at 19:54 UTC
    Crossposted to StackOverflow. It's considered polite to inform about crossposting to prevent wasting the efforts of people not attending the other site where solution might already exist.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Honestly didn't know, and will now follow those rules. I do find them rather overly strict there. They closed my question even though I assumed that I made it clear that the above code worked. I've had people downvote me with a "Why would you want to do that?" From the old forums days I do recall the waste of time with people who were to lazy to even try, or just wanted people to do their coding homework (for school) for them. But still.... Although, I wonder if your comment means that they reopened my question?????
      But my question, even though I fixed the OP is still closed there.
Re: Why am I uploading nothing? $upload_filehandle appears to read nothing
by Corion (Patriarch) on Feb 13, 2022 at 00:02 UTC

    Your code is a bit incomplete as the saysOutput function is missing. I assume that it is something like:

    sub saysOutput { my( @message ) = @_; print "Content-Type: text/html\r\n\r\n"; print join "\r\n", @message; };

    I would first try the snippet in the Processing-a-file-upload-field of CGI documentation for copying an uploaded file:

    # undef may be returned if it's not a valid file handle if ( my $io_handle = $q->upload('field_name') ) { open ( my $out_file,">$upload_dir/$filename" ) or die "Couldn't create upload file: $!"; while ( my $bytesread = $io_handle->read($buffer,1024) ) { print $out_file $buffer; } }
Re: Why am I uploading nothing? $upload_filehandle appears to read nothing
by Anonymous Monk on Feb 13, 2022 at 11:01 UTC