Database Reference
In-Depth Information
print start_multipart_form ( - action => url ()),
"Image name:" , br (),
textfield ( - name => "image_name" , - size => 60 ),
br (), "Image file:" , br (),
filefield ( - name => "upload_file" , - size => 60 ),
br (), br (),
submit ( - name => "choice" , - value => "Submit" ),
end_form ();
When the user submits an uploaded file, begin processing it by extracting the parameter
value for the file field:
$file = param ( "upload_file" );
The value for a file upload parameter is special in CGI.pm because you can use it two
ways. You can treat it as an open file handle to read the file's contents or pass it to
uploadInfo() to obtain a reference to a hash that provides information about the file
such as its MIME type. The following listing shows how post_image.pl presents the form
and processes a submitted form. When first invoked, post_image.pl generates a form
with an upload field. For the initial invocation, no file will have been uploaded, so the
script does nothing else. If the user submitted an image file, the script gets the image
name, reads the file contents, determines its MIME type, and stores a new row in the
image table. For illustrative purposes, post_image.pl also displays all the information
that the uploadInfo() function makes available about the uploaded file:
#!/usr/bin/perl
# post_image.pl: enable user to upload image files using post requests
use strict ;
use warnings ;
use CGI qw(:standard escapeHTML) ;
use Cookbook ;
print header (), start_html ( - title => "Post Image" );
# Use multipart encoding because the form contains a file upload field
print start_multipart_form ( - action => url ()),
"Image name:" , br (),
textfield ( - name => "image_name" , - size => 60 ),
br (), "Image file:" , br (),
filefield ( - name => "upload_file" , - size => 60 ),
br (), br (),
submit ( - name => "choice" , - value => "Submit" ),
end_form ();
# Get a handle to the image file and the name to assign to the image
my $image_file = param ( "upload_file" );
my $image_name = param ( "image_name" );
Search WWH ::




Custom Search