in reply to Uploading files: can't find contents?
When the form is processed, you can retrieve the entered filename by calling param(): $filename = $query->param('uploaded_file'); Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below). The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls: # Read a text file and print it out while (<$filename>) { print; } # Copy a binary file to somewhere safe open (OUTFILE,">>/usr/local/web/users/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; } However, there are problems with the dual nature of the upload fields. If you "use strict", then Perl will com- plain when you try to use a string as a filehandle. You can get around this by placing the file reading code in a block containing the "no strict" pragma. More seriously, it is possible for the remote user to type garbage into the upload field, in which case what you get from param() is not a filehandle at all, but a string. To be safe, use the upload() function (new in version 2.47). When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle. $fh = $query->upload('uploaded_file'); while (<$fh>) { print; } In an array context, upload() will return an array of filehandles. This makes it possible to create forms that use the same name for multiple upload fields. This is the recommended idiom.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: •Re: Uploading files: can't find contents?
by tachyon (Chancellor) on Apr 04, 2003 at 01:01 UTC | |
by merlyn (Sage) on Apr 04, 2003 at 01:03 UTC | |
by maksl (Pilgrim) on Apr 04, 2003 at 15:22 UTC | |
by jasonk (Parson) on Apr 04, 2003 at 15:24 UTC |