Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Problems with uploading a file from a web form

by ezekiel (Pilgrim)
on Nov 12, 2002 at 01:56 UTC ( [id://212171]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to upload a file from a web form. I've read How can I get started with a file upload script? and, in the spirit of that answer, here is what I have:

The form is generated by a template (of which this is a snippet) as such:

<form method="get" action="./source_definition" enctype="multipart/for +m-data"> <p><input type="file" name="upload_file"></p> </form>

Then the snippet of code that handles the upload looks like this:

my $filename = $cgi->param('upload_file'); binmode($filename); my $test = ""; my $buffer; while ( read($filename, $buffer, 1024) ) { $test .= $buffer; }
The goal is to have the whole file in $test. The problem is that at the end of it $test is empty. What have I missed?

Thanks

Replies are listed 'Best First'.
Re: Problems with uploading a file from a web form
by cLive ;-) (Prior) on Nov 12, 2002 at 02:18 UTC
Re: Problems with uploading a file from a web form
by dingus (Friar) on Nov 12, 2002 at 07:40 UTC
    You need to POST not GET on the form. This is yet another reason to ALWAYS USE CGI.PM as it handles all this sort of thing for you and automagically gets it right.

    Also take a look at the relevant example source code and try it out.

    Dingus


    Enter any 47-digit prime number to continue.
Re: Problems with uploading a file from a web form
by Sihal (Pilgrim) on Nov 12, 2002 at 13:41 UTC
    Here is a portion of code that uploads a file using HTTP::Request::Common and LWP::UserAgent:
    my $req = POST "$url",Content_Type => 'form-data',Content => [ user_id + =>$usr{user_lc},password => $usr{password},session_id => $usr{sessi +on_id},image1 => ["$image_path1"] ];
      oops already answered by dingus
Re: Problems with uploading a file from a web form
by iburrell (Chaplain) on Nov 12, 2002 at 16:59 UTC
    If you want to read the whole file into a string, then you should read in one go instead of reading it piece by piece and growing the string. This is done by setting $/ to undef and reading from the filehandle. The standard idiom is:
    my $test = do { local $/; <$filename> };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://212171]
Approved by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found