FW: [thelist] Upload feature......

Roee Rubin webdesign1 at irubin.com
Fri Sep 7 20:00:19 CDT 2001


>> I've been wading through all sorts of stuff on TheList waiting and
>> hoping for exactly that snippet, and you're replying OFF LIST?
>> Arrgh!  No!!  Say it ain't so!!

Sorry.

See below:

<snippet filename="upload.pl">

#!/usr/bin/perl
use CGI;


$|=1;
$co = new CGI;

$DIR = "/path/to/my/upload/directory/";

################
#file upload routine
################


#######
# Generate random name. Used so that no two files will be uploaded with the
same name.


@chars = ( "A" .. "Z", 0 .. 9 );
$randomname = join( "", @chars[ map { rand @chars } ( 1 .. 8 ) ] );

#####

chop $DIR if ($DIR =~ /\/$/);
my ($Filename);
foreach $key (sort {$a <=> $b} $co->param()) {
	next if ($key =~ /^\s*$/);
	next if ($co->param($key) =~ /^\s*$/);
	next if ($key !~ /^File-(\d+)$/);
	$Number = $1;

	if ($co->param($key) =~ /([^\/\\]+)$/) {
		$Filename = $1;
		$Filename =~ s/^\.+//;
		$File_Handle = $co->param($key);
	}

	($ext) = $Filename =~ m#/?.*[.]([^/.]+)$#;

	#substitue $randomname to $Filename to upload real file name

	open(OUTFILE, ">$DIR\/$randomname.$ext");

	undef $BytesRead;
	undef $Buffer;

	while ($Bytes = read($File_Handle,$Buffer,1024)) {
		$BytesRead += $Bytes;
		binmode OUTFILE;
		print OUTFILE $Buffer;
	}
	close($File_Handle);
	close(OUTFILE);

}

########

print $co->header();
print $co->start_html();
print "upload completed!";
exit;

</snippet>


Below is the html page. You may have as many file input fields as desired as
long as name format is kept.


<snippet filename="whatever.html">

<html>
<head>
<title>Upload File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="mainform" action="/path/to/my/perl/file/" method="post"
enctype="multipart/form-data">
  <p>File Upload</p>
  <p>
    <input type="FILE" name="File-01" size="30">
  </p>
  <p>
    <input type="submit" name="Submit" value="Upload">
  </p>
</form>
</body>
</html>

</snippet>


Roee Rubin
Irubin Consulting
roee at irubin.com
Phone (310) 402-0120
http://www.irubin.com/


-----Original Message-----
From: Kevin Martin [mailto:kevin at brasscannon.net]
Sent: Friday, September 07, 2001 5:27 PM
To: webdesign1 at irubin.com
Subject: RE: [thelist] Upload feature......


At 05:53 AM 9/7/01, you wrote:


>I can send you a snippet of code in perl that does just this.
>
>Let me know ...

I've been wading through all sorts of stuff on TheList waiting and
hoping for exactly that snippet, and you're replying OFF LIST?
Arrgh!  No!!  Say it ain't so!!

Please share!






More information about the thelist mailing list