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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to (jcwren) Re: Uploading a file by jcwren
in thread Uploading a file by wonko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found