Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

(Ovid) Re: file upload freaking me out

by Ovid (Cardinal)
on Jan 06, 2001 at 09:40 UTC ( #50213=note: print w/replies, xml ) Need Help??

in reply to file upload freaking me out

There are a few problems with what I see above. First, it looks like your code is incomplete. Did you try to "pare it down" when posting? Amongst other things, you have while (<$file_name>), but you never opened $file_name.

Problems with the following snippet:

open (F, ">$photo_save_name"); while (<$file_name>) { print F; }
  • You should test whether or not "open" was successfful.
  • As I mentioned, you didn't open $file_name.
  • Further, you haven't reset $/, so after you open $file_name, you'll be breaking $file_name on the newlines.
  • while (<$file_name>) should be written as:
    while ( read( $file_handle, $buffer, BUFFER_SIZE ) ) {
Those are the major things that I see, so perhaps you can work on those, first.

Incidentally, I share your pain about and file uploads. One thing you might want to try is a new install and ensure that you've installed ALL of the modules with it. Not upgrading all of them can have unpredictable results (I speak from experience).


Update: Of course he didn't open $file_handle. He didn't need to. I've written enough upload scripts that I should have paid attention. I am so embarrassed. :)

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: file upload freaking me out
by chipmunk (Parson) on Jan 06, 2001 at 09:59 UTC
    Trimbach did not open $file_name because the CGI module actually handles that part:
    CREATING A FILE UPLOAD FIELD ... When the form is processed, you can retrieve the entered filename by calling param(). $filename = $query->param('uploaded_file'); ... 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; } ...
    There's also an example on checking the mime type:
    ... When a file is uploaded the browser usually sends along some information along with it in the format of headers. The information usually includes the MIME content type. Future browsers may send other information as well (such as modification date and size). To retrieve this information, call uploadInfo(). It returns a reference to an associative array containing all the document headers. $filename = $query->param('uploaded_file'); $type = $query->uploadInfo($filename)->{'Content-Type'}; unless ($type eq 'text/html') { die "HTML FILES ONLY!"; }
Re: (Ovid) Re: file upload freaking me out
by Trimbach (Curate) on Jan 06, 2001 at 09:56 UTC
    I didn't open $file_name because my $file_name = param('photo'); yields both a string with the uploaded filename AND an open filehandle to the uploaded file. Explicitly opening it isn't necessary (and it works as-is.) The only thing I pared down from the posted code was the error-handling and name-generating parts of the code.

    Testing the open (F, ">$photo_save_name");, although a good idea, isn't a problem. This is the code that works, remember.

    My new code does replace while (<$file_name>) with while ( read( $file_handle, $buffer, BUFFER_SIZE ) ) { (but, really, with 30K files and a 16K buffer I'm not really gaining much) but again, that's not what's giving me fits (none of those things break anything.) Why doesn't $CGI::POST_MAX work? Why doesn't upload(), tmpFileName(), and uploadInfo() work? Sigh. And yeah, I checked my CGI installation and installed all the subclasses before posting. Again, sigh.

    Gary Blackburn
    Trained Killer

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://50213]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-05-25 21:38 GMT
Find Nodes?
    Voting Booth?