NAME

OpenInteract2::Upload - Represent a file upload

SYNOPSIS

my $request = OpenInteract2::Request->get_current;

# Get the upload as listed under 'input_file'
my $upload = $request->upload( 'input_file' );

# Get information about the upload
print "Filename: ", $upload->filename, "\n",
      "Cleaned filename: ", $upload->clean_filename( $upload->filename ), "\n",
      "Size: ", $upload->size, " bytes\n",
      "Content-type: ", $upload->content_type, "\n";

# Dump the data uploaded to a file the long way...

open( NEW, "> blah" ) || die "cannot open blah: $!";
my ( $data );
my $filehandle = $upload->fh;
binmode NEW;
while ( read( $filehandle, $data, 1024 ) ) {
    print NEW $data;
}

# ...or the short way

$upload->save_file;                  # use a cleaned up version of the
                                     # filename specified by the user
$upload->save_file( 'newname.foo' ); # specify the filename yourself

DESCRIPTION

This class defines an object representing a generic uploaded file. The OpenInteract2::Request subclasses must define one of these objects for each file uploaded from the client.

METHODS

Class Methods

new( \%params )

Defines a new upload object. The \%params can be one or more of the PROPERTIES.

base_filename( $filename )

Removes any leading directories from filename. Web clients will frequently include the full path when sending an uploaded file, which is useless. This is called automatically when you call the filename() set method.

clean_filename( $filename )

Removes a number of 'bad' characters from filename. This is not called automatically.

Object Methods

save_file( [ $filename ] )

Saves the filehandle associated with this upload object to $filename. An exception is thrown if $filename is outside $website_dir or if there is an error writing the file.

You can leave off $filename and OpenInteract will save the file to the 'upload' directory set in your server configuration. This directory must be writeable by the process the server runs under.

See OpenInteract2::File for the heuristic used for finding a filename to save the content.

Returns: the full path to the filename saved

PROPERTIES

All properties have a corresponding combined get/set method.

name: Name of the field that holds the uploaded file. If you are using an HTML form to get the file from the client, this is the name of the FILE field.

filename: Name of the file as reported by the client. Note that whenever you call the set method the value supplied is run through base_filename() before being set into the object. (Alias: fh)

filehandle: A filehandle reference suitable for reading the uploaded file.

content_type: The MIME content type as reported by the client. (Be warned: sometimes the client lies or is just plain ignorant.) (Alias: type)

size: The size of the uploaded file, in bytes.

BUGS

None known.

TO DO

Nothing known.

SEE ALSO

Apache::Request

CGI

COPYRIGHT

Copyright (c) 2002-2004 Chris Winters. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHORS

Chris Winters <chris@cwinters.com>