http://qs1969.pair.com?node_id=17066


in reply to Uploading a file

What, exactly, is the question? Are you looking for code to do this, an explanation of the framework for how to do this, or general ideas for the user interface?

I recently had to do something similiar. I manage (such as one does) a large database of batteries. Users need to be able to upload pictures of batteries in to the database. There is a page with a couple of fields on, where the user enters the serial number, describes the picture (intact, exploded, top view, whatever), and, most importantly, the path to the file on their local machine. This is done using a INPUT field, with a type of FILE.

When the user clicks 'Go', the file will be submitted as part of the POST data. Note that the ENCTYPE on the FORM with the INPUT button must be set to "multipart/form-data".

Let's say that your input field is named 'photofile'. The following code
my $fn = $html->param ('photofile'); my $fh = $html->upload ('photofile');
gets the filename the user wants to upload from his local drive, and a file handle to said file. You then proceed to use a loop
# # This could get a little hairy. We don't know how big a picture +the user i # give us. We're going to limit him to 512K. That should cover a +nything we # of. # while ((length ($photo) <= (512 * 1024)) && read ($fh, $buffer, 102 +4)) { $photo .= $buffer; } if (length ($photo) == (512 * 1024)) { print_file ("File too big!"); return; }
to read the data into a string. Now I perform some other processing on the file data before it's loaded into the database, but you could copy it straight to a file (I pump the image through ImageMagick to create a thumbnail, do some normalization, and limit the dimensions of the picture). You *absolutely* need to set a limit on how much data you'll accept or some weenie may try to upload a 1,000,000 x 1,000,000 x 24 bit image of Britney Spears to see how you'll handle it.

This is the basic form of what you're trying to do. If you can clarify the question some, a little more specific help can probably be provided.

--Chris

Replies are listed 'Best First'.
RE: Re: Uploading a file
by Anonymous Monk on Jun 08, 2000 at 17:06 UTC
    Hmmm ... 1,000,000 x 1,000,000 x 24 bit image of Britney Spears!!!