in reply to Re^2: File Upload On Windows 8 and Perl
in thread File Upload On Windows 8 and Perl

Gut instinct with nothing to back it -- can you give me exactly what is in the form fields at the time you click the submit button?

Where I'm going with this: It seems fairly likely that it is opening the file and then dying for some reason, thus leaving you with an empty file. Perhaps it's having trouble parsing the input filename, in some manner which is not made visible to you despite all the wonderful efforts in the example to get the errors back to you; or once opened, it is having trouble finding a channel back to the input data from your file.

If nothing pops out at me with your supplied data, maybe I can spin up a quick version of the file uploader as you have done and troubleshoot it from this side.

  • Comment on Re^3: File Upload On Windows 8 and Perl

Replies are listed 'Best First'.
Re^4: File Upload On Windows 8 and Perl
by skosterow (Novice) on Jul 04, 2015 at 16:06 UTC

    Here ya go guys/gals:

    HTML:

    <form action="/test/test.pl" method="post" ENCTYPE="multipart/form-dat +a"> <INPUT TYPE="file" NAME="photo"> <INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Submit Form"> </form>

    CGI:

    use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw( :standard); use File::Slurp qw( read_file ); use DBI; use Crypt::Lite; use File::Basename; ############################################### # CGI IN my $query = CGI->new; my $submit = $query->param('SUBMIT'); my $user_id= $query->param('USER_ID'); my $filename = $query->param("photo"); my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "\images-user"; $CGI::POST_MAX = 1024 * 5000; ############################################### if ($user_id eq '') { my $cookie = CGI->new; $user_id = $cookie->cookie('TEC_USER_ID'); my $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex +8' ); my $decrypted = $crypt->decrypt($user_id, $ip_address) +; $user_id = $decrypted; } #################################################### ##################### Main Page #################### #################################################### # Start Main Page if ($submit eq '') { #code } elsif ($submit eq 'Submit Form') { if ( !$filename ) { print $query->header ( ); print "There was a problem uploading your photo (try a smaller fil +e)."; 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 { die "Filename contains invalid characters"; } my $upload_filehandle = $query->upload("photo"); open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; }

    Hope that helps!

      It sure did:

      my $upload_dir = "\images-user";

      Embedded backslashes need to be doubled:

      my $upload_dir = "\\images-user";

      Will continue checking but that one jumped right out at me.

        when i change that i get a no such file or directory - I think it has to do with defining the string with a " and not a '.

      The docs for CGI say the upload can return undef if there was a problem and you should call it like this :-

      my $file = $q->upload( 'uploaded_file' ); if ( !$file && $q->cgi_error ) { print $q->header( -status => $q->cgi_error ); exit 0; }
        okay i added this and get no errors! :)