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

I'm trying to write a cgi script to allow file uploads. I can't seem to get upload() to define my filhandle scalar. I am able to write $filename to the upload directory but it conatains 0K size.

The documentation says that upload uses /tmp, /usr/tmp and then where the script is to write the temporary file that the filehandle links to.

I just don't see what I'm missing, this looks and seems pretty straight forward.

Thanks for any and all help
Joe

Code Snipet:

#!/usr/bin/perl -w use strict; use CGI; my $formdata = CGI->new; my $file = $formdata->param('file'); my $filename = $formdata->param('filename'); my $fh = $formdata->upload($file); print STDERR "$file\n"; print STDERR "$filename\n"; if (defined $fh){<br> print STDERR "\$fh is defined\n"; }

Apache Error Log:

test.jpg new_test.jpg [Sat Nov 1 17:49:29 2003] [error] [client 127.0.0.1] Premature end of + script headers: /var/www/cgi-bin/eface/clients.pl

Replies are listed 'Best First'.
Re: Upload Question
by antirice (Priest) on Nov 02, 2003 at 00:03 UTC

    Why are you using $file for the upload field?

    # name entered by the user for the uploaded file my $file = $formdata->param('file'); # the actual filehandles for the uploaded file my $fh = $formdata->upload('file');

    Obviously $file and $filename are getting printed. Since "$fh is defined" wasn't printed, we know it is undefined. Why is it undefined? Unless $file is 'file' or there's an upload field called 'test.jpg', $fh will be undefined. As for the "Premature end of script headers" error, I noticed that you didn't print a header. Thus the error message.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      I thought my $fh = $formdata->upload($file); and my $fh = $formdata->upload('file'); are the same.

      Guess not since the latter returns a defined value for $fh.

      I'm still ending up with a file in my upload directory but it still contains 0K for size.

      Guess I'll try working this out now that I have a defined filehandle.

      Sorry about the Premature end...., I'm just trying to get the upload() thing worked out.

      Joe

        The Creating a File Upload Field portion of the pod for CGI.pm should give you some ideas. Just skip the first couple of paragraphs until you get to a paragraph that begins, "The filename returned is...". Just replace $filename with $fh in the examples he gives. However, be certain to read the entire section in case the OS you're using this on distinguishes between text and binary data modes.

        Hope this helps.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Upload Question
by jZed (Prior) on Nov 01, 2003 at 23:58 UTC
    You didn't print a content-header to the browser. Try print $formdata->header(); before your first print statement. Also, there's no code to save the file on the server.
Re: Upload Question
by adrianh (Chancellor) on Nov 02, 2003 at 15:33 UTC