CGI – Upload Files

In our daily development, sometimes we need to upload files to our Unix file system (~username). For example, my username is luhuang, then my location is /home/luhuang and I can visit /home/luhuang/public_html using URL http://www-xxxxx.com/~luhuang/. Usually we will use ftp command to upload files to a Unix machine which provides ftp services and can be accessed from my Unix file system.

For example:

I need to upload C:/work/abc.txt to /home/luhuang/public_html,

1. CMD-> cd to C:/work, then issue: ftp appxxx@unix123machine

2. put abc.txt

3. from Unix /home/luhuang/public_html, scp appxxx@unix123machine:/slot/xxxx/abc.txt .

4. dosunix abc.txt

To save time, I composed a tiny upload script to do that. Below is the scripts:

upload.pl (Upload Form)

#!/local/bin/perl

#C:\Perl\strawberry\perl\bin\perl.exe

use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;

$CGI::POST_MAX = 1024 * 5000000;

my $query = new CGI;

print $query->header ( );
print <<END_HTML;
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>File Upload Application</title>
<style type=”text/css”>
img {border: none;}
</style>
</head>
<body>
<p>File Upload Application</p>
<form action=”upload.cgi” method=”post”
enctype=”multipart/form-data”>
<p>File to Upload: <input type=”file” name=”file” /></p>
<p>Upload Location: <input type=”text” name=”location” /></p>
<p><input type=”submit” name=”Submit” value=”Submit Form” /></p>
</body>
</html>
END_HTML

 

upload.cgi (upload handler)

#!/local/bin/perl

#C:\Perl\strawberry\perl\bin\perl.exe

use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;

$CGI::POST_MAX = 1024 * 5000000;

my $query = new CGI;
my $filename = $query->param(“file”);
my $upload_dir = $query->param(“location”);
my $link = “”;

if ( !$filename )
{
print $query->header ( );
print “Failed to upload file: $! \n”;
exit;
}

my ( $name, $path, $extension ) = fileparse ( $filename, ‘\..*’ );
$filename = $name . $extension;
$filename =~ s/ /_/g;

if (length($upload_dir) < 1){
$upload_dir = “/home/luhuang/public_html/upload”;
$link = qq(<a href=\”http://www-xxxx/~luhuang/upload/$filename\”>$filename</a>);
}

my $upload_filehandle = $query->upload(“file”);

open ( UPLOADFILE, “>$upload_dir/$filename” ) or die “$!”;
binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;

system(“chmod 777 $upload_dir/$filename”);
system(“dos2unix $upload_dir/$filename”);

print $query->header ( );
print <<END_HTML;
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>File Upload Application</title>
<style type=”text/css”>
img {border: none;}
</style>
</head>
<body>
<p>Uploaded Successfully</p>
<p>Location: $upload_dir/$filename $link </p>
</body>
</html>
END_HTML

 

Output:

cgi_upload