Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Can someone supply me with a simple perl script for uploading a file via a browser from the client PC to a local directory of the server.
Thank You.

Replies are listed 'Best First'.
Re: File Upload
by atcroft (Abbot) on Sep 11, 2004 at 14:08 UTC

    If I may suggest, try either the search at the top of the page, or the site's Super Search. I got several results that may be of use by trying searches for file upload, picture upload (because that is a common use for such scripts), and upload. While the applicability of any one post may vary, I think you may be able to find several that might provide you with a good starting point.

    Hope that helps.

Re: File Upload
by Velaki (Chaplain) on Sep 11, 2004 at 14:21 UTC

    If you can run CGI.pm programs from the web server, then you should be able to use upload() method of CGI.pm to grab the filehandle of the uploaded file, whereupon the rest of the program can write it to whatever directory you may.

    As for an entire script, there are a many wonderful examples out there, so I'll leave it to you to chose the one that best suits your needs.

    Hope this helped,
    -v
    "Perl. There is no substitute."
Re: File Upload
by bradcathey (Prior) on Sep 11, 2004 at 14:33 UTC

    Okay, couldn't resist. Here's one that I have tweaked over the years:

    sub uploader { $| = 1; #flush the output buffer my $sourcefile = $query->param('uploadimage'); #make sure your HTML + form tag uses enctype="multipart/form-data" my ($buffer, $bytes, $newfile); if ($sourcefile =~ /([\w .-]+)$/i) {; #strip off path stuff $newfile = $1; } else { return("bad"); #returns to main prog for error handling } $newfile =~ s/ //; open (OUTFILE, ">$directory/$newfile") or die "Cannot open $newfile +: $!"; binmode($sourcefile); binmode(OUTFILE); while ($bytes = read($sourcefile, $buffer, 1024)) { print OUTFILE $buffer; } close(OUTFILE) or die "Close:$!"; chmod (0666, "$directory/$newfile"); }

    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
Re: File Upload
by sintadil (Pilgrim) on Sep 11, 2004 at 15:47 UTC

    Can someone supply me with a simple perl script for uploading a file via a browser from the client PC to a local directory of the server.

    I echo Velaki's suggestion in that there are plenty of ready-made examples available, but I must advise you to avoid Matt's Script Archive, otherwise known as MSA. Matt has admitted to producing inferior, insecure, and downright buggy code. A good place to start may be the NMS archive, which provides higher quality CGI scripts.