Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attaching file to a webform using formmail.pl

Status
Not open for further replies.

anders123

Programmer
Joined
Jun 11, 2003
Messages
7
Location
SE
I have a webform in which I want the users to be able to attach a file. I am using formmail.pl from and I want to modify it so it handels file attachments. How is it done?

/anders
 
Dunno how you would modify the 'webform' script to do this, but to give you an example, I took a CGI perl script from the mod_perl test section that does file uploads and modified it (heavily) to allow binary files (and handle windows pathnames as well), it is as follows (and stores the files in the ./files directory of where this script sits). It's ugly, but works! Hope it helps:

use strict;
use CGI qw(:standard);
use CGI::Carp qw/fatalsToBrowser/;
use Apache;
use File::Basename;
$|=1;

my $r = Apache->request();

print header();
print start_html("Upload Files"),
h1("Upload Files"),
start_multipart_form(),
"Enter the file to process:",
filefield('filename','',45),
br,
reset,submit('submit','Process File'),
endform;

if (my $file = param('filename')) {
my $filename="";
if ($file =~ /.*\/.*/)
{
$filename = dirname($r->filename) . "/files/$file";
print &quot;$filename<br>&quot;;
}
elsif ($file =~ /.*\\.*/)
{
my @stuff=split /\\/, $file;
chomp @stuff;
$filename= dirname($r->filename) . &quot;/files/$stuff[(scalar @stuff) - 1]&quot;;
print &quot;$filename<br>&quot;;
}
else
{
$filename = dirname($r->filename) . &quot;/files/$file&quot;;
}
open (OUTFILE, &quot;>$filename&quot;) || die &quot;$0: Could not open $filename: $!\n File to upload was $file\n&quot;;
my $bytes=undef;
my $buffer=undef;
while ($bytes=read($file,$buffer,1024))
{
print OUTFILE $buffer;
}
close OUTFILE;
print hr,h1,basename($filename),&quot; uploaded&quot;;
}

print end_html;


---
Batavus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top