in reply to Upload Question

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

Replies are listed 'Best First'.
Re: Re: Upload Question
by Anonymous Monk on Nov 02, 2003 at 02:07 UTC
    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

        Here is where I am at now:

        #!/usr/bin/perl -w use strict; use CGI; use Fcntl; my $formdata = CGI->new; my $file = $formdata->param('file'); my $filename = $formdata->param('filename'); #user supplied fi +le name my $fh = $formdata->upload('file'); my $upload_dir = "/var/www/html/eface/client_pics/"; my $buffer = ""; print $formdata->header(); print STDERR "$file\n"; print STDERR "$filename\n"; if (defined $fh){ print STDERR "\$fh is defined\n"; } # Open the file to be wriiten into the upload directory sysopen OUTPUT, $upload_dir . $filename, O_CREAT | O_EXCL; # Write file to upload directory while (read ($fh, $buffer, 16384)){ print OUTPUT $buffer; } close OUTPUT;

        This code still gives me the file named $filename in my upload directory $upload_dir but it still contains 0K byte size.

        Now when I run in my code:

        while (<$fh>){ print; }

        it does print out data to the browser so I know that the temporary file is being found. I can't quite solve this. Any Ideas?

        Thanks Again for everyones help.

        Joe