Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: Thatís silly. Itís a string op tool.
[Lady_Aleena]: So I guess I'll go argue with File::Find and grep.
[Your Mother]: I would probably use Path::Tiny instead for it's slurp and such.
[Your Mother]: s/it's/its/

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2017-05-23 19:39 GMT
Find Nodes?
    Voting Booth?