Home > Articles > Web Development > Perl

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Handling File Uploads

You need to store files uploaded from HTML forms.


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->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>");

 # Output a simple form.
 <form enctype="multipart/form-data" name="files" action="/upload" 
  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">

 return OK;


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



The filename associated with this upload.


An open filehandle you can use to read the uploaded file.


-Additional HTTP headers sent by the client, accessible as an Apache::Table object.


The name of the form field containing the file.


Size of the file, in bytes.


Name of temporary spool file created on disk.


The MIME type of this file, as determined by the client.

  • + Share This
  • 🔖 Save To Your Account