- Introduction
- The Apache Request Object
- The HTTP Request Message
- The Client Request
- Accessing Client Request Headers
- Accessing HTML Form Fields
- Reading POSTed Data Manually
- Manipulating Cookies
- Handling File Uploads
- Setting Server Response Headers
- Controlling Caching Behavior
- Sending Server Response Headers
- Setting the Response Status
- Setting Error Headers
- Manipulating Headers with Multiple Like Fields
- Using Subrequests
- Setting Headers for Subrequests
- Short-Circuiting Subrequests
- Getting or Setting the Request Method
- Accessing the Request Object from XS
Handling File Uploads
You need to store files uploaded from HTML forms.
Technique
Use the upload() method provided by Apache::Request. It returns Apache::Upload objects that contain information about the uploaded file and provide access to the file data itself.
package Cookbook::PrintUploads; use Apache::Constants qw(OK); use Apache::Request; use Apache::Util qw(escape_html); use strict; sub handler { # Standard stuff, with added options... my $r = Apache::Request->new(shift, POST_MAX => 10 * 1024 * 1024, # in bytes, so 10M DISABLE_UPLOADS => 0); my $status = $r->parse(); # Return an error if we have problems. return $status unless $status == OK; $r->send_http_header('text/html'); $r->print("<html><body>\n"); $r->print("<h1>Upload files</h1>"); # Iterate through each uploaded file. foreach my $upload ($r->upload) { my $filename = $upload->filename; my $filehandle = $upload->fh; my $size = $upload->size; $r->print("You sent me a file named $filename, $size bytes<br>"); $r->print("The first line of the file is: <br>"); my $line = ; $r->print(escape_html($line), "<br>"); } $r->print("Done......<br>"); # Output a simple form. $r->print(<<EOF); <form enctype="multipart/form-data" name="files" action="/upload" method="POST"> File 1 <input type="file" name="file1"><br> File 2 <input type="file" name="file2"><br> File 3 <input type="file" name="file3"><br><br> <input type="submit" name="submit" value="Upload these files"> </form> </body></html> EOF return OK; }; 1;
Comments
Processing uploads requires a few small changes to the way we have been doing things with Apache::Request. Looking at our example, you'll notice that we are adding a few parameters to the call to Apache::Request->new(). To enable uploads, we set the DISABLE_UPLOADS option to 0. We also set POST_MAX to a sensible value; in this case, 10 megabytes. Next we call the parse() method to process the form data, including the uploaded files. If there are problems with the upload, they will surface here as a bad return code, suitable for returning or comparing to values from Apache::Constants. Additionally, an error message accessible through the notes() interface
my $errmsg = $r->notes("error-notes");
is provided which can be used in a custom response, another handler, or when logging.
After verifying that there were no errors during the file upload, the next step is to call Apache::Request's upload() method. upload() returns one or more Apache::Upload objects depending on its context. If called in a list context, as in the preceding example, the upload() method returns a list of all the files the user uploaded as Apache::Upload objects. In a scalar context with a form field name as an argument, it will return the specific file (if it exists).
my $upload = $r->upload('treasure');
If you have a valid Apache::Upload object, you can access the uploaded file and all sorts of information related to it. Table 3.5 summarizes the most frequently used methods from the Apache::Upload class. For a complete list see the Apache::Request documentation.
Table 3.5 Some Apache::Upload Methods
Method Name |
Description |
filename() |
The filename associated with this upload. |
fh() |
An open filehandle you can use to read the uploaded file. |
info() |
-Additional HTTP headers sent by the client, accessible as an Apache::Table object. |
name() |
The name of the form field containing the file. |
size() |
Size of the file, in bytes. |
tempname() |
Name of temporary spool file created on disk. |
type() |
The MIME type of this file, as determined by the client. |