Perlfect Solutions

Handling file uploading from www forms with


Netscape Navigator 2.0 introduced a new facility for forms, the file upload element. This element allows the user to select a local file to be submitted with the form to the server that handles the form. File uploading is a frequently needed facility in many occasions, but the technique involved in handling forms that upload files is rarely discussed in CGI programming texts, and online tutorials/references on this topic are few. The technique is very simple as we shall see.

The form

First of all we'll need to prepare the HTML form that will contain the file upload element: <FORM ENCTYPE="multipart/form-data" ACTION="/cgi-bin/" METHOD="POST"> Please choose directory to upload to:<br> <SELECT NAME="dir"> <OPTION VALUE="images">images</OPTION> <OPTION VALUE="sounds">sounds</OPTION> </SELECT> <p> Please select a file to upload: <BR> <INPUT TYPE="FILE" NAME="file"> <p> <INPUT TYPE="submit"> </FORM>

Which will look on the browser like that:
Please choose directory to upload to:

Please select a file to upload:

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

The CGI script

On the server side, we'll have the CGI program ( that will handle the upload. We will use that does all the dirty job for us. processes the submission of the form for us and provides us with a simple method of retrieving each individual part of the submission. In our case we only have one element, called file:

1 #!/usr/bin/perl 2 use CGI; 3 my $cgi = new CGI; 4 my $dir = $cgi->param('dir'); 5 my $file = $cgi->param('file');

Now it is quite obvious what the value of $dir will be. But what will the value of $file be assigned to? ACtually $file now is two things:
  1. In a scalar context it is the filename on the client computer, as it appears in the file selection box.
  2. When used in a filhandle context, it behaves as a read-mode filhandle from which the uploaded file can be read just like any normal file opened locally. (actually what happens here is that keeps the upload in a temporary file and provides us with an open handle to it so we can read through it)
And that's all there is to it. Now all we got to do is read in the file and store it somewhere or do whatever else we might want to do with it.

6 $file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename 7 my $name = $2; 8 open(LOCAL, ">$dir/$name") or die $!; 9 while(<$file>) { 10 print LOCAL $_; 11 } 12 print $cgi->header(); 13 print "$file has been successfully uploaded... thank you.\n"; First of all, because the filename that we get from the form includes the full path of the file on the remote machine we need to strip that part. Lines 6-7 do just that. Next we open a local file and we just iterate through the upload input and dump its contents to it. Finally we print out a message back to the browser to confirm that the upload has been successful.

Online Documentation/Tutorials

  • The POD documentation for has a section explaining all you need to know about file uploading.

Save This Page


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'perlfect'@'' (using password: YES) in /home/content/g/i/o/giorgoszervas/html/comments/comments_include.php on line 6
Connection Error: Access denied for user 'perlfect'@'' (using password: YES)

Like it? Share it!

Post to
Post to

Suggested Reading

Official Programming Guide The Official Programming Guide is the definitive manual and guidebook for writing CGI programs with perl and the CGI library. While the manual distributed with the library as part of perl's documentation is well written and covers almost anything you'd need to know about using, this book is a useful companion for anyone making CGI scripts with perl.
CGI Programming CGI Programming is an introductry book for CGI programming, perhaps not the best book I've read. It covers most topics about the CGI protocol and how to write server side programs to work with it. Nevertheless, most if not all of the information in this book (as with most books that discuss CGI programming) can be found in tutorials and references on the web, but if you feel like buying a book anyway, you may want to consider this one.
Webmaster In A Nutshell Webmaster in a nutshell is a catch-all reference book for webmaster and programmers. It does not have anuything that you can't find online, but if you're liek me you might want to have all the stuff you refer to frequently nicely laid out in a well-organized book lying on your desk. If you're looking for something like that then you'll be happy with this book.
Perl Cookbook The Perl Cookbook is full of quick solutions to everyday programming problems in perl with explanations and tips easy to understand even for beginners, but also frequently useful even to more experienced programmers. The code is clear and straightforward and the topics covered as well-thought and correspond to real world examples, so frequently you can literally copy code snippets from the book and fit them in your program. It is a nice complement for the Camel Book on your bookshelf.