The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


Apache::Upload - Methods for dealing with file uploads.


    use Apache::Upload;

    $req = Apache::Request->new($r);
    $upload = $req->upload("foo");
    $size = $upload->size;

    # three methods to get at the upload's contents ... slurp, fh, io


    read $upload->fh, $fh_data, $size;
    ok $slurp_data eq $fh_data;

    my $io = $upload->io;
    print while <$io>;


Apache::Upload is a new module based on the original package included in Apache::Request 1.X. Users requiring the upload API must now use Apache::Upload, which adds the upload method to Apache::Request. Apache::Upload is largely backwards-compatible with the original 1.X API. See the "PORTING from 1.X" section below for a list of known issues.

This manpage documents the Apache::Upload and Apache::Upload::Brigade packages. Apache::Upload::Table and Apache::Upload::Error are also provided by this module, but are documented elsewhere. For a list of related manpages, read the "SEE ALSO" section below.




The name of the HTML form element which generated the upload.



The (client-side) filename as submitted in the HTML form. Note: some agents will submit the file's full pathname, while others may submit just the basename.



Creates filehandle reference to the upload's spooled tempfile, which contains the full contents of the upload.



Creates a tied IO handle. This method is a more efficient version of fh, but with io the handle ref returned is not seekable. It is tied to an Apache::Upload::Brigade object, so you may use the brigade API on the tied object if you want to manipulate the IO stream (beyond simply reading from it).

The returned reference is actually an object which has read and readline methods available. However these methods are just syntactic sugar for the underlying READ and READLINE methods from Apache::Upload::Brigade.

    $io = $upload->io;
    print while $io->read($_); # equivalent to: tied(*$io)->READ($_)

See READ and READLINE below for additional notes on their usage.



Get/set the APR::Brigade which represents the upload's contents.



Returns the size of the upload in bytes.



Get/set the additional header information table for the uploaded file. Returns a hash reference tied to the APR::Table class. An optional $table argument can be passed to reassign the upload's internal (apr_table_t) info table to the one $table represents.

    my $info = $upload->info;
    while (my($hdr_name, $hdr_value) = each %$info) {
        # ...

    # fetch upload's Content-Type header
    my $type = $upload->info->{"Content-type"};



Returns the MIME type of the given Apache::Upload object.

    my $type = $upload->type;

    #same as
    my $content_type = $upload->info->{"Content-Type"};
    $content_type =~ s/;.*$//ms;

To avoid recopying the upload's internal tempfile brigade on a *nix-like system, link will create a hard link to it:

  my $upload = $req->upload('foo');
  $upload->link("/path/to/newfile") or
      die sprintf "link from '%s' failed: $!", $upload->tempname;

Typically the new name must lie on the same device and partition as the brigade's tempfile. If this or any other reason prevents the OS from linking the files, link() will instead copy the temporary file to the specified location.



Reads the full contents of a file upload into the scalar argument. The return value is the length of the file.

    my $size = $upload->slurp($contents);



Provides the name of the spool file.

    my $tempname = $upload->tempname;


This class is derived from APR::Brigade, providing additional methods for TIEHANDLE, READ and READLINE. To be memory efficient, these methods delete buckets from the brigade as soon as their data is actually read, so you cannot seek on handles tied to this class. Such handles have semantics similar to that of a read-only socket.



Creates a copy of the bucket brigade represented by $bb, and blesses that copy into the Apache::Upload::Brigade class. This provides syntactic sugar for using perl's builtin read, readline, and <> operations on handles tied to this package:

    use Symbol;
    $fh = gensym;
    tie *$fh, "Apache::Upload:Brigade", $bb;
    print while <$fh>;


    $bb->READ($contents, $length)
    $bb->READ($contents, $length, $offset)

Reads data from the brigade $bb into $contents. When omitted $length defaults to -1, which reads the first bucket into $contents. A positive $length will read in $length bytes, or the remainder of the brigade, whichever is greater. $offset represents the index in $context to read the new data.



Returns the first line of data from the bride. Lines are terminated by linefeeds (the '\012' character), but we may eventually use $/ instead.

PORTING from 1.X

  • $upload->next() is no longer available; please use the Apache::Upload::Table API when iterating over upload entries.

  • info($header_name) is replaced by info($set).

  • type() returns only the MIME-type portion of the Content-Type header.


Apache::Upload::Table, Apache::Upload::Error, Apache::Request, APR::Table(3)


  Copyright 2003-2005  The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  See the License for the specific language governing permissions and
  limitations under the License.