CERN - European Organization for Nuclear Research     Web Services  
Friday, 26 April 2024
 

4. "Uploading Files using HTML Forms"

Uploading and receiving files from within CGI-script is non-trivial, so be aware that you have to know what you are doing, otherwise consider to leave it !!! 
 
There is no support for examples given within this site, they should merely serve as starting points to demonstrate some basic methods and principles !!!
 
In any case you might want to consider using the central Web-servers
where advanced techniques like uploading files using HTML-forms etc
can be done very easily !!!
(for more info see http://webservices.web.cern.ch/WebServices/docs/Advanced/Upload/)

You need 2 parts for uploading files using a perl CGI script

1) The form

You need to create the HTML form that will contain the file upload element correctly:

<p>
 <FORM ENCTYPE="multipart/form-data" ACTION="cgi-bin/form-upload.pl" METHOD="POST">
   Please select a file to upload: <br> 
<INPUT TYPE="FILE" NAME="file">
<br>
<INPUT TYPE="submit">
</FORM>
</p>


Notice the form parameter ENCTYPE="multipart/form-data" which determines the encoding that will be used to submit it.


2.) The CGI script to handle the form-upload

Within your Web-site the CGI program (cgi-bin/form-upload.pl) will handle the file upload. You better use CGI.pm that encapsulates most of the job for us. Below is the corresponding Perl example for the form above:


#!/usr/local/bin/perl

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $tmpDir = $ENV{"PATH_TRANSLATED"};  $tmpDir =~ m|(.*)\\cgi-bin|; my $siteBase = $1; 
my $UploadDirectory = "$siteBase\\upload"; 
# next  line for Windows/IIS sites only (temporary directory where web-server also needs write access)
$TempFile::TMPDIRECTORY="$siteBase\\tmp";;

my $query = new CGI; 
my $file = $query->param('file');     
#  let's extract the filename
$file=~m/^.*(\\|\/)(.*)/;          #separate path and filename
 my $fileName = $2;                        # the matching part in the second pair of brackets

open(LOCALFILE, ">$UploadDirectory\\$fileName") or die $!;   # exit if you cannot create local file 
while(<$file>) {
print LOCALFILE $_;
}
close LOCALFILE;
close $file;   

print $query->header();
print "<html><body><h1>The file '$file' has been uploaded ... thank you.</h1></body></html>\n";

 


ad) my $tmpDir = $ENV{"PATH_TRANSLATED"};  $tmpDir =~ m|(.*)\\cgi-bin|; my $siteBase = $1;
my $UploadDirectory = $siteBase . "
\\upload";

NB:The account under which the webserver is running needs write access to the relevant directory(ies).

Windows / IISLinux / Apache
The account that needs write access is IUSR_Webservices. You have to create according sub-webs using FrontPage and grant write (authoring) permissions for them to this account.The account for the AFS gateway web-server is within the AFS protection group webserver:afs. So the command

fs sa . webserver:afs write

has to be issued in the upload directory !!!
.

 


ad "my $file = $query->param('file'); "

The parameter "$file" you receive has two functions

a) in a scalar context it is the filename as seen from the submitting client computer
b) is a filehandle to read the files contents


 

For more information on the CGI module see: http://www.perldoc.com/perl5.6/lib/CGI.html

 

Continue to: 5. Sending Mail from a CGI-script !

Send feedback to: Web Support
Last update: Thursday, October 02, 2003 05:15:51 PM
Web Services
© CERN /
IT / Internet Services