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

My platform is apache 2 on Solaris. I'm trying to use the Uploadify plugin to allow file uploads to my CGI::Application. I can monitor the runmode being called, I can print the file as it uploads but I can't save the file.
sub Upload{ my $self=shift; my $q=$self->query(); warn "In upload runmode"; my $buffer; my $bufsize=1024; my $bytes_retrieved=0; my $fh=$q->upload('upfile'); my $filename=$q->param('upfile'); my $tmp = File::Temp->new( DIR=$self->cfg('UploadDirectory'); SUFFIX=>'.zip'); warn "$tmp"; open (TEMPFILE, '>', \$tmp) || die $!; binmode TEMPFILE; while (read ($fh, $buffer, $bufsize)){ #warn "$buffer"; #i can see the file here print TEMPFILE $buffer; $bytes_retrieved += $bufsize; } close TEMPFILE; warn "now check the upload directory"; return; }
The permissions are correct for the upload directory. I don't get any warnings or errors in the apachelog, excluding the ones I put in for debugging. Any help would be great:)

Replies are listed 'Best First'.
Re: File uploads, CGI::Application, File::TEMP and Uploadify
by almut (Canon) on Apr 29, 2010 at 15:11 UTC
    ...but I can't save the file.

    The temp file is automatically unlinked (when the handle goes out of scope), unless you tell it otherwise with UNLINK => 0.  Also, you don't need to open another TEMPFILE handle (which, btw, doesn't help with the unlink issue), as $tmp already is an opened handle to the file...  I.e. try

    my $tmp = File::Temp->new( DIR => $self->cfg('UploadDirectory'), SUFFIX => '.zip', UNLINK => 0, ); # warn $tmp; # stringification gives filename ... print $tmp $buffer;
      Hey, you rock! Thank you for the advice! Do I need to binmode this somehow? I'm uploding zip files from windows, the file gets created but I can't unzip it, it claims to be corrupt.
      while (read ($fh, $buffer, $buffsize)) { print $tmp $buffer; $bytes_retrieved += bufsize; }
        Do I need to binmode this somehow?

        The documentation says the temp file is already being opened in binary mode, so theoretically, there should be no need to binmode it yourself.  You can of course still try it, though... (just say binmode $tmp; before printing to it.)  Ditto for the file handle $rh that you're reading from.

Re: File uploads, CGI::Application, File::TEMP and Uploadify
by Anonymous Monk on Apr 29, 2010 at 11:56 UTC
      Thanks for the tip, sorry for not being clear first time. Uploadify is a jquery/flash plugin (http://www.uploadify.com/). With my warnings I can see the file being uploaded, I can't seem to save it on the server.