Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

CGI file upload

by Reims (Initiate)
on Aug 01, 2013 at 11:12 UTC ( #1047433=perlquestion: print w/replies, xml ) Need Help??
Reims has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I'm trying to get a file upload script to work. To savethe uploaded file, I do this:
my $fh = $q->upload($filefield_name); #check for valid file handle if (defined $fh) { my $io = $fh->handle; open(OUTFILE, '>>', '/srv/www/cgi-bin/upload'); while ($bytesread == $io->read($buffer, 1014)) { print OUTFILE $buffer; } print "File uploaded"; } else { print "file handle not defined"; }
Executing that will give me this error:
Undefined subroutine Fh::handle at /srv/www/cgi-bin/fo_www/document_add.cgi line 51
After a couple of hours of finding loads of tutorials telling me to do exactly that, I don't know what I'm doing wrong.

Replies are listed 'Best First'.
Re: CGI file upload
by marinersk (Priest) on Aug 01, 2013 at 11:36 UTC
    Interesting; I am unfamiliar withe the file handling mechanism you use; while I would love to advise you on that, I'll leave that to others.

    Here is a snippet showing how I do my file uploads:

    my $inpfsz = 0; open(RESFIL, ">$desfnm"); binmode(RESFIL); while (my $inpcnt=read($srcfnm,$inpbuf,2096)) { $inpfsz += $inpcnt; print RESFIL $inpbuf; } close(RESFIL);

    You can find more detail, including some mandatory advice about the HTML that drives the upload, at

Re: CGI file upload
by Anonymous Monk on Aug 01, 2013 at 12:33 UTC
      Thanks! Using $cgi->tmpFileName does the trick for me. Greetings Reims
Re: CGI file upload
by Anonymous Monk on Aug 01, 2013 at 12:34 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047433]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-05-27 16:50 GMT
Find Nodes?
    Voting Booth?