in reply to Bad file descriptor when uploading file via CGI

My first suggestion is to use the newer upload method in CGI.pm so you don't have to deal with the magic-ness of the filehandle/string duality. It may not help but it removes a potential source of problems. The other issue is that this code
read($sourcefile, $buffer, 1024) or die "Cannot read sourcefile: $!"
looks a little funny to me, because read can return zero (a false value) when it reaches the end of the file but still not hit an error condition. That's all I can think of for now.

Replies are listed 'Best First'.
Re^2: Bad file descriptor when uploading file via CGI
by bradcathey (Prior) on Apr 07, 2005 at 08:07 UTC

    Thanks for the upload() tip. I found an example here at the monastery and plugged it into my code. Worked wonderfully.

    Here's my revised code:

    $| = 1; my ($file,$path) = @_; my $filename = $query->upload($file); my $buffer; open(OUTPUT, ">$path/$filename"); binmode($filename); binmode(OUTPUT); while( read($filename, $buffer, 64*2**10) ) { print OUTPUT $buffer; } close(OUTPUT); return ($filename);

    I would still like to know what the original problem was, but right now I just need a solution.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot