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

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!

Replies are listed 'Best First'.
Re^5: File Upload On Windows 8 and Perl
by marinersk (Priest) on Jul 04, 2015 at 16:09 UTC

    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 '.

        DANGER, WILL ROBINSON!

        Messing around with backslashes requires detailed understanding of how they behave in both single and double quoted strings.

        If it is failing with doubled backslashes, that's a clue, because in double-quotes, that's the correct interpretation. So I am compelled to ask you to slow down to answer this one:

        What is the fully qualified path to the directory you want the download to wind up in?
        Not in PerlSpeak -- in ShellSpeak/CommandPromptSpeak.

        My gut still says this is a player in this drama -- we could be overlooking something really, really basic here.

Re^5: File Upload On Windows 8 and Perl
by RichardK (Parson) on Jul 04, 2015 at 16:49 UTC

    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! :)

        Okay, well, that eliminates an error discoverd by the CGI module, which is actually a huge relief in many ways.

        After cross-checking all your absolute vs. relative filepath issues, I'm inclined to say try the TEMPoverride. Again, even if it merely eliminates that as a source of the issue, it's progress of a sort.