display | more...

Uploading a file to your server with a CGI script is dead easy with perl. That's beacuse the CGI module conviently passes file input types as both a file name and a file handle. This means you can read the contents of the file like you'd read any other file handle.

So, if the HTML form looks like this (note the enctype='multipart/form-data', it's what allows your browser to send an entire file):

<form action='cgi.pl' method='post' enctype='multipart/form-data'>
    <input type='file' name='filename'>                           
</form>        

Then you can read and save the file passed with:

#!/usr/bin/perl -w

use strict;
use CGI;  
use FileHandle;

my $cgi = CGI->new();
my $filename = $cgi->param( 'filename' );
                        
my $fh = FileHandle->new( ">/tmp/temporary.file" ) || die "$!";

while (<$filename>) { 
    print $fh;
}   

If you're on Windows NT or something, you might need to set the filehandle to binmode.

Log in or register to write something here or to contact authors.