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

Hi there, I am in need of a little bit of help. I am attempting to write a file up-load CGI script to go with a form I've written, and I can't seem to get the file up-load to work properly.

Let me show you what I mean, the code below is the up-load section of a test CGI script I wrote:

my ( $fil, $bts, $bfr, $dta, $tru, ); $fil = $cgi->param('file'); while ($bts = read($fil, $bfr, 1024) || croak "Oops! $!") { $dta .= $bfr; } $hdr = $cgi->header({type=>"text/html"}); $cnt .= $cgi->start_html(); $cnt .= $cgi->p("Here's the data: "); $cnt .= $dta; $cnt .= $cgi->end_html(); print $hdr, $cnt; exit 0;

Now ... when the form is submitted, the script croaks with this error:

"Software error: Oops! Bad file descriptor at test.cgi line 30" ... "Bad file descriptor"

it happens with .txt files, .doc files, .jpg files, etc.

Could someone kindly fill me in on what exactly I'm doing wrong? I'd appreciate your time.

The web serever is Apache2, running on Kubuntu Linux Dapper Drake.

Thank you again for you time, I'm stumped.

-s0up

Code tags added by GrandFather

Visit us at crazylogic.org #apartment411 D A R K E M P I R E s u b n e t w o r k

Replies are listed 'Best First'.
Re: Up-Loading Files From Multi-Part Form
by sgifford (Prior) on Sep 14, 2006 at 05:04 UTC
    A few suggestions:
    • Try using the upload method if it's available in your version of CGI; that's the recommended interface nowadays.
    • Add some error checking to see if a file is being uploaded, and if that is detected correctly. That may well get you to the source of the problem, and even if not it will make your code more robust.
    • The fileno function will tell you the file descriptor of a file handle, and might be helpful in debugging.

    Good luck!

Re: Up-Loading Files From Multi-Part Form
by zentara (Cardinal) on Sep 14, 2006 at 11:06 UTC
    The module CGI::Simple makes it easy.
    #!/usr/bin/perl use warnings; use strict; use CGI::Simple; my $maxsize = 1024 * 100; #max 100K my $upload_dir='uploads'; my $q = new CGI::Simple; print $q->header(); if($ENV{CONTENT_LENGTH} > $maxsize){ print "file too large - must be less than $maxsize bytes"; exit; } my $files = $q->upload(); # number of files uploaded +; my @files = $q->upload(); # names of all uploaded fil +es my $filename = $q->param('upload_file'); # filename of uploaded fil +e my $mime = $q->upload_info($filename,'mime'); # MIME type of uploa +ded file my $size = $q->upload_info($filename,'size'); # size of uploaded f +ile # short and sweet upload my $ok = $q->upload( $q->param('upload_file'),"$upload_dir/$filename") +; print "Uploaded ".$q->param('upload_file')." and wrote it OK!\n" if $o +k; print "total files = $files<br> filenames = @files<br> filename = $filename<br> mimetype= $mime<br> size=$size<br>";

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Up-Loading Files From Multi-Part Form
by gellyfish (Monsignor) on Sep 14, 2006 at 07:59 UTC