<?xml version="1.0" encoding="windows-1252"?>
<node id="130392" title="Answer: I need a simple web upload script." created="2001-12-08 05:48:57" updated="2005-08-14 16:11:25">
<type id="1888">
categorized answer</type>
<author id="11732">
QandAEditors</author>
<data>
<field name="doctext">
&lt;p&gt;
Below is some code I wrote a while back for doing something similar, though not necessarily exactly the same.
Below that is the HTML form, so you can see the &lt;B&gt;enctype&lt;/B&gt; entry.
&lt;/p&gt;&lt;p&gt;
There are some issues with this code, such as the chance of files being overwritten, but this should give you a starting point.
&lt;/p&gt;&lt;p&gt;
This script also lets the uploader send an email. Of course, this could be abused, so some restrictions on this should probably be put in place.
&lt;/p&gt;&lt;p&gt;
Relevant thread: [id://129949].
&lt;/p&gt;&lt;P&gt;
&lt;B&gt;My post-upload.cgi script:&lt;/B&gt;
&lt;CODE&gt;
#!/usr/bin/perl --

use CGI qw( :standard escapeHTML );
use strict;

my $megabyte = 1024 * 1024;  # bytes
my $max_mb   = 10; # max no. of MB we will allow

$CGI::DISABLE_UPLOADS = 0;  
# CGI module variable for en/disabling uploads (non-zero to disable)
$CGI::POST_MAX = $megabyte * $max_mb;  
# CGI module variable for maximum upload size (bytes)

my $base_dir = "/var/www/site1/web/";
my $base_dom = "http://www.website.here/";
my $target_dir = "../uploads";
my $directory = $base_dir . $target_dir;
my $sendmail = "/usr/bin/sendmail";
my @sendmail_opts = ( '-oi', '-t' );

$| = 1;
my $query = new CGI;

my @names = $query-&gt;param;
my $url = $query-&gt;param("URL");
my $fh = $query-&gt;upload('upload_file');
my $filename = $query-&gt;param('name');
$filename =~ s/[^A-Za-z0-9\.\_]/_/g;

open OUTF, "&gt; $directory$filename" or die;
binmode OUTF;
while ( $bytesread = read $fh, $buffer, 1024 )
{
	print OUTF $buffer;
}
close OUTF;

if ( !$file &amp;&amp; $query-&gt;cgi_error )
{
	print $query-&gt;header( -status =&gt; $query-&gt;cgi_error );
	exit 0;
}


open MAIL, "| $sendmail @sendmail_opts" or die "Can't fork sendmail: $!\n";

print MAIL
    "From: $0\n",
    "To: ", $query-&gt;param("TO"), "\n",
    "Subject: ", ( $query-&gt;param("SUBJECT") || "mailed form submission" ),
    "\n\n";

for my $param ( @names )
{
	printf MAIL "%s: %s\n", $para, $query-&gt;param($param);
}

print MAIL
    "\n\nThe link to the uploaded file is $base_dom$target_dir$filename . ",
    "It was renamed by the upload script, so you will need to rename it.\n";

close MAIL;

print $query-&gt;redirect( -URL =&gt; $url );
&lt;/CODE&gt;
&lt;/p&gt;
&lt;P&gt;
&lt;B&gt;My sample upload form (body only):&lt;/B&gt;
&lt;CODE&gt;
&lt;form action="/cgi-bin/post-upload.cgi" method="post" enctype="multipart/form-data"&gt;
&lt;!-- hidden inputs --&gt;
&lt;input name="TO" type="hidden" value="my.address@my.domain" /&gt;
&lt;input name="URL" type="hidden" value="http://www.website.here/submission.done.htm" /&gt;
&lt;input name="SUBJECT" type="hidden" value="From Your Upload Center on Website" /&gt;
&lt;!-- visible inputs --&gt;
File to send: &lt;input name="upload_file" type="file" size="30" /&gt;&lt;br&gt;
   Your name: &lt;input name="name" size="42" /&gt;&lt;br&gt;
      E-mail: &lt;input name="Email" size="42" /&gt;&lt;br&gt;
&lt;input type="submit" value="SEND FILE" /&gt;
&lt;input type="reset" value="CLEAR" /&gt;
&lt;/form&gt;
&lt;/CODE&gt;
&lt;/p&gt;
</field>
<field name="parent_node">
129822</field>
</data>
</node>
