Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Having an issue with my CGI.pm file uploads

by technojosh (Priest)
on Sep 25, 2007 at 16:02 UTC ( [id://640979]=perlquestion: print w/replies, xml ) Need Help??

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

I have a web tool that manages a set of java files. A feature of this tool is being able to upload java files, or update existing ones(also via an upload).

I had this script working (i thought), but now all uploaded files show up blank(0 bytes), and from this bit of code:

while ( <$upload_filehandle> ) { print UPLOADFILE; }
I get the following errors in my apache log:

Use of uninitialized value in <HANDLE>
readline() on unopened filehandle

Below is the complete code that handles the upload. The script seems to get the file info, because it does open a new file of the same name in the target directory, it just shows up as 0 bytes. I am wondering why $upload_filehandle is not getting opened correctly?
Environment: win32, apache server

use CGI; my $i = new CGI; my $ieFileMod; my $upload_filename = $i->param( 'JARFILE' ); my $upload_filehandle = $i->upload( 'JARFILE' ); # handle copying the file to server $upload_filename =~ m/^.*(\\|\/)(.*)/; # strip the remote path and kee +p the filename # still need to do something if its IE submitted file, since above reg +ex doesn't clean up filename for IE if( $upload_filename =~ /\\/ ) { my @nameList = split( /\\/, $upload_filename ); $ieFileMod = pop( @nameList ); } my $finalName = $upload_filename; if( $ieFileMod ) { $finalName = $ieFileMod; } my $localFile = "java\\$finalName"; # copy the jar file in binary mode to server open UPLOADFILE, ">$localFile"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;

Replies are listed 'Best First'.
Re: Having an issue with my CGI.pm file uploads
by Joost (Canon) on Sep 25, 2007 at 17:26 UTC
      Thank you for the help, my form does have those elements already...
Re: Having an issue with my CGI.pm file uploads
by toolic (Bishop) on Sep 25, 2007 at 16:30 UTC
    Did you check if $upload_filehandle is defined before using it?
    my $upload_filehandle = $i->upload( 'JARFILE' ); print "upload_filehandle not defined\n" unless (defined $upload_fileha +ndle);
      OK, if I add that print statement, you are correct...

      It isn't getting defined.

      So then, is my problem the upload function in CGI.pm? The call:

      my $upload_filename = $i->param( 'JARFILE' );
      successfully returns the filename, but the call:
      my $upload_filehandle = $i->upload( 'JARFILE' );
      is apparently the issue?
        The documentation on CGI offers some reasons why upload might return an undef value. You've already exceeded my experience with this module. Perhaps wiser monks have better advice.
        I am having the same issue. Did you find out the problem?
Re: Having an issue with my CGI.pm file uploads
by mr_mischief (Monsignor) on Sep 25, 2007 at 16:36 UTC
    If you weren't getting the file opened, it shouldn't exist, even at zero bytes. You might want to test the return of the open() call and print any errors to make sure.
    open my $output, '>', $localFile || die "Cannot write to $localfile: $!\n";
    What's the size of the uploaded file? Are you sure it's longer than zero bytes in the temp directory after the upload? Perhaps it's a permissions issue with the upload. Why are you using binmode() on the output but not the input filehandle? You have no counter to see how may times you get lines in your while loop nor any test of the length of the lines. How can you be sure you're not writing exactly what there is to write?
      I am using binmode due to the filetypes that could be uploaded. I also think its odd that the file exists, but sure as heck it does, at 0 bytes, in the correct directory on the web server

      I do see your point about not testing the file, and it is valid. I probably cut some corners there, but only because I also am in control of the files that get uploaded.
      The one I'm testing with is ~7kb in size.

        You're not using binmode() on $upload_filehandle, though. You want to make sure you read in binary mode, too.

        As for file size, if you have binary data in the input file but it's not in binary mode, you could have issues there.

        Also, make sure you have a version of CGI.pm that doesn't have the fairly recent broken upload problem. CGI 3.21 and 3.22 simply don't work for uploads. See Skip CGI.pm versions 3.21 and 3.22 when upgrading -- they break the upload() method for more info. It could be you're not getting a file uploaded because the library really is broken.

        What does print $CGI::VERSION; give you?

Re: Having an issue with my CGI.pm file uploads
by roboticus (Chancellor) on Sep 25, 2007 at 16:23 UTC
    technojosh:

    You forgot to open $upload_filehandle....

    ...roboticus

    Update: I missed it ... I was looking for open statements, not a call. In that case, I'd suspect the upload function.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://640979]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (9)
As of 2024-03-28 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found