This document tells how to code for uploading a file from the client machine to a Unix server and viewing the contents of the uploaded file using PERL-CGI technology.
The steps to be followed for uploading a file from client machine to Unix server and displaying the contents of the uploaded file:
- Uploading the file from client machine to Unix server
- Viewing the content of the uploaded file
Uploading the file:
This involves two steps:
- § § Throwing an html page with a file selector for selecting the file from the PC, which is to be up loaded in the server.
- § § Saving the file in the server
Code for the html page for selecting the file to be loaded and submitting the same:
################################################
##The enctype in the <FRAME> tag should be multipart/form-data
<FORM NAME=”fileupload” METHOD=POST ACTION=Uploader.cgi enctype=”multipart/form-data”>
FIRST_PART
##Check whether the upload is successful or not
if($retcod ne undef)
{
if ($retcod == 0)
{
print “<FONT size=4>Ran File Uploader successfully.</FONT><BR><BR><HR>”;
}
else
{
print “<FONT size=4>Error while file uploading.</FONT><BR><BR><HR>”;
}
}
print “<BR>”;
print “Please Select the file to be loaded: \n”;
##File Selector
print “<input type=file size=30 name=acct_file maxlength=100 >\n”;
print “<BR>”;
print “<BR>”;
##Submitting the form after selecting the file to be uploaded in the file selector
##check_file_ext() is the function to check whether any file is selected in the file selector or not
print “<INPUT type=SUBMIT NAME=Submit VALUE=SUBMIT onClick=’return check_file_ext(document. fileupload.acct_file.value); ‘ >\n”;
print “ \n”;
print “<INPUT type=RESET NAME=Reset1 VALUE=RESET>\n”;
##This form is used for viewing the uploaded file
<FORM name=fileupload1 method=post action = upload_file.cgi>
<B>For Viewing the Uploaded File: </B><input type=submit name=viewlog value=$viewlog><BR>
In the above code Uploader.cgi saves the uploaded file in the server. The code of the same is given below.
For Uploading a file the most important point is:
In the <FORM> tag the enctype should be equal to “multipart/form-data”.
The output of the above is:
Code for saving the up loaded file in the server:
$path = “infile_test”;
$path_res = “resfile_test”;
open (FILE, “>$path”);
open (RFILE, “>$path_res”);
$writefile=0;
##For parsing the content of the file to be saved in the server
while ($j = <STDIN>)
{
if ($j ne undef)
{
$j =~ s///g;
print FILE $j;
if ($j =~ /”; filename=”/)
{
$start=1;
next;
}
if ($start==1)
{
if ($j =~ /—————————–/)
{
$start=0;
next;
}
if ($j ne “\n”)
{
if ($j !~ /Content-Type:/)
{
$j =~s/^M//g;
print RFILE $j;
$writefile=1;
}
}
}
}
else
{
exit;
}
}
close(FILE);
close(RFILE);
‘rm $path’;
##Saving the uploaded file in the server with the name “fileUpload”
$path_trg=”fileUpload”;
$retCode=0;
print “<BR>”;
print “<B><Font color=$EQCOLOR size=5>PLEASE WAIT, FILE UPLOAD IS IN PROGRESS…….</FONT></B>”;
##Check whether the file is uploaded successfully
if ($writefile==1)
{
‘mv $path_res $path_trg’;
}
else
{
$retCode=1;
‘rm $path_res’;
if(-f $path_trg)
{
‘rm $path_trg’;
}
}
$viewlog = “VIEWCONTENT”;
##After saving the file in the server it goes back to the same page from where the file was submitted for uploading
print “<FORM method=’POST’ name=upload action=fileUpload.cgi >”;
print “<INPUT TYPE=hidden NAME=filename VALUE=$path_trg>\n”;
$RETCODE=”retcode”;
print “<INPUT TYPE=hidden NAME=$RETCODE VALUE=$retCode>\n”;
The above code helps in parsing the file, which is to be loaded in the server, and after parsing is completed it saves the file in the server. The parsing can be done in several ways. The above is one of the ways. According to the document to be up loaded the parsing code can be modified by using PERL script.
The output of the page while saving in the server is:
Viewing the Content of the Up loaded file:
Viewing the content of the up loaded file is done from the same web page from where the file is submitted for up loading. This searches for the file uploaded in the server with the name the up loaded file is saved in the server. Then it reads the uploaded file and displays the content of the same in a web page.
The code for the same as follows:
##The uploaded file
$uploadfilename=”fileUpload”;
open (FILE,$uploadfilename);
print “<BR>”;
print “<B>”;
$writeflag=0;
##Reading the Uploaded file
while ($j = <FILE>)
{
if ($j ne undef)
{
$writeflag=1;
$j=~ s/^\s*(.*?)\s*$/$1/;
print “$j”;
print “<BR>”;
}
else
{
exit;
}
}
##Throws the error message with the absence of uploaded file
if ($writeflag == 0)
{
print “THE UPLOADED FILE DOES NOT EXIST!”;
}
The output of the above code for a test file, which is uploaded in the server:
Tags: coding, file uploader, File Uploader using Perl, Perl, technical FAQ, technical tips