in reply to Uploading files: can't find contents?

Perhaps you should follow the advice of the manpage:
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: &bull;Re: Uploading files: can't find contents?
by tachyon (Chancellor) on Apr 04, 2003 at 01:01 UTC

    Actually he is explicitly following this advice merlyn (making the presumption that $this->cgi is a CGI object). No doubt he will be missing the ENCTYPE="multipart/form-data" in the FORM TAG or have uploads disabled $CGI::DISABLE_UPLOAD or be exceeding $CGI::POST_MAX.

    One of the less user friendly things about CGI.pm is that if misses the opportunity to report the cause of failing file uploads and leave a useful message in err_str()

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      how could i access $CGI::DISABLE_UPLOAD ?
      #!/usr/bin/perl -w use CGI; print "CGI::DISABLE_UPLOADS: $CGI::DISABLE_UPLOADS\n";
      gives:
      Name "CGI::DISABLE_UPLOAD" used only once: possible typo at cgi-bin/te.pl line 3.
      Use of uninitialized value in concatenation (.) at cgi-bin/te.pl line 3.
      CGI::DISABLE_UPLOAD:
      thx maksl

      Update:
      the other name variable gives CGI::POST_MAX: -1, perhaps my version of CGI can't disable upload?

      Update2:
      added plural to above, thx jasonk .. this results in CGI::DISABLE_UPLOADS: 0

        It's plural... $CGI::DISABLE_UPLOADS.


        We're not surrounded, we're in a target-rich environment!