Perl example |
![]() ![]() ![]() |
The example below demonstrates how using a Perl script you can process files submitted by HTTP File Upload Applet. The source code for this example may be found in the examples/perl/receiver.pl file included with the HTTP File Upload Applet distribution.
Example
#!/usr/bin/perl
# use CGI module
use CGI;
use File::Path;
# define location where uploaded files will be stored, notice that permissions to write are needed for upload directory
$uploaddir = '/var/uploads/';
$query = new CGI;
# parse the name of file
$filename = $query->param("file");
# Index where starts the file name
$index = rindex($filename,"\\");
# Gets the directory path name from the file name
$dirName = substr($filename, 0,$index+1);
# Check the directory name
if($dirName ne ""){
# Concat the directory path name
$uploaddir = $uploaddir . $dirName;
# Make the directory if it does not exist
if(! -d $uploaddir){
mkpath $uploaddir;
}
}
$filename =~ s/.*[\/\\](.*)/$1/;
# gets file handle
$upload_filehandle = $query->upload("file");
# reads contents and saves it out
open UPLOADFILE, ">$uploaddir/$filename" || &upload_error;
# tells Perl to write the file in binary mode, rather than in text mode.
binmode UPLOADFILE;
while ( <$upload_filehandle> ) {
print UPLOADFILE;
}
# close the file
close UPLOADFILE;
print $query->header();
print "RESP.100";
sub upload_error {
print INFO "****ES ERROR***\n";
print "RESP.200";
exit;
}
Installation
1. | Change $upload_dir variable in receiver.pl script to point to directory path where file should be uploaded. DO NOT include a / at the end of this path. |
2. | Upload receiver.pl to the CGI directory on your web server. Typically this directory is named cgi-bin. Please consult your system administrator if you are unsure where this directory is located. |
3. | Add execute permissions to the receiver.pl script. You can do this by connecting to the web server using telnet or ssh, navigating to the CGI directory and issuing the command chmod 755 receiver.pl |
4. | Set url parameter in HTML applet code to point to url of this script e.g. http://www.jscape.com/cgi-bin/receiver.pl |
5. | Done. You may now run HTTP File Upload Applet |
See also