Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Upload files with CGI::Application
Upload files with CGI::Application
This tutorial is about how to upload files from a HTML form with CGI::Application.
First read the CGI::Application tutorial if you don't know how the module works.

If you plan to create a form not from a HTML template but from a query object, check out the section
CREATING A FILE UPLOAD FIELD
in the documentation for CGI.pm:

    # perldoc CGI

NOTE: A HTML form containing a file field must be encoded as a multipart form:
<form method="post" action="myapp.pl" enctype="multipart/form-data">
<input type="file" name="uploaded_file" size=50>
.
.
.
The file name can be retreived as any form field, using the query object:
$filename = $query->param('uploaded_file');
$filename is also a file handle.

You can print the contents of an uploaded file with:
while ($bytesread = read($filename, $buffer, 1024)) {
  $output .= $buffer;
}
return $output;
or save the uploaded file:
open (OUTFILE, ">>/usr/local/apache/htdocs/news.html");
while ($bytesread = read($filename, $buffer, 1024)) {
  print OUTFILE $buffer;
}
Be careful when permitting uploads to the server.
You have to consider to untaint the filename if you are running in taint mode (which you should).
See Perl taint mode for details.
You should also set a file size limit, and always use binmode together with the open statement to permit users uploading any type of file (both text and binary files, which are treated in different ways on MS Windows), as shown in the example below:
# Retreive the uploaded file
my $bytes_retreived = 0;
my $bufsize = 1024;
my $limit = 1024 * 1024; # 1 MB limit
my $buffer = '';
# Open tempfile for writing
open (TMPFILE, ">$fullpathfilenametmp");
binmode TMPFILE; # To be sure MS Windows binary files don't get corrupted.
while (($bytes_retreived <= $limit) && read ($fh, $buffer, $bufsize))
  {
    print TMPFILE $buffer;
    $bytes_retreived += $bufsize;
  }
close TMPFILE;
# Failure (file too big), remove tempfile
if ($bytes_retreived >= $limit)
  {
    $error .= "Error: File <b>$filename</b> too big. (max size = " . filesizesuffix($limit) . ")<br>";
    unlink $fullpathfilenametmp
      or $error .= "Error: Deleting uploaded part of too big file failed: $!<br>";
  }
# Success, rename tempfile to uploaded file
else
  {
    rename $fullpathfilenametmp, $fullpathfilename
      or $error .= "Rename file failed: $!<br>";
    $msg .= "File <b>$filename</b> uploaded succesfully.<br>" unless $error;
  }
}

Check the following files for a working demo for printing an uploaded file's contents to the browser:
  • uploadapp.pl, the application wrapper
  • UploadApp.pm, the application module
  • uploadapp1.tmpl.html, the 1st screen's HTML template
  • uploadapp2.tmpl.html, the 2nd screen's HTML template
The demo itself should look like this.

Last modified: Tue Sep 6 11:40:18 EDT 2016