Click to See Complete Forum and Search --> : File Upload through Form
firefighter2049
11-22-2005, 10:44 PM
I am working on a form that collects user input and emails though a simple HTML form. I want to be able to allow the user to upload a file to be sent along with the rest of the form information. Everything works except the file side. Any suggestions on how to code the file upload?
This is what I've got, but I know something is missing. Just not sure what.
Upload a JPG or GIF file from your computer:<br></b></font><input type=hidden name=max_file_size value=5000000> <input type=file size=40 name=userfile>
Jeff Mott
11-23-2005, 05:35 AM
Refer to the CGI module's documentation (http://perldoc.perl.org/CGI.html#CREATING-A-FILE-UPLOAD-FIELD) for accessing the uploaded file within the script and the MIME::Lite (http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm#Attach_a_GIF_to_a_text_message) module for attaching the file to an email.
Nedals
11-23-2005, 02:12 PM
My guess...
You forgot to include...
enctype="multipart/form-data"
in your <form> tag
firefighter2049
11-23-2005, 10:45 PM
Thanks for the help so far. I did forget that tag. I still can't quite figure this out though. I can see the name of the file, but it isn't attaching. Here's the form:
<FORM ACTION="/bin/script_library/form_handler_mail" METHOD=post enctype="multipart/form-data" >
What's your name?
<INPUT TYPE="text" NAME="name"><BR>
What's yout email address?
<INPUT TYPE="text" NAME="email">
<BR>
<input type=hidden name="state" value="changePic"><input type=hidden name="rand" value="84"><tr valign=middle><td align=center><font size=2><b>Upload a JPG or GIF file from your computer:<br></b></font><input type=hidden name=max_file_size value=5000000> <input type=file size=40 name=userfile><br><input type=submit name=uploadFile value="Upload File">
And here is what I get as the result:
The following data was submitted via your form:
email: gfhfgh
max_file_size: 5000000
name: gfhgfh
rand: 84
state: changePic
uploadFile: Upload File
userfile: C:\Documents and Settings\Owner\Desktop\favicon.ico
No actual attatchment. I glanced throught the MIME info, but will admit it is over my head.
gozarca2
12-11-2005, 12:35 PM
you've got the actual data, but you need to open the file in your cgi/php and read it, like so:
#!/opt/perl/bin/perl
use CGI qw/:standard/;
# DO NOT REMOVE. You must have CGI.PM module on your server. (MOST SERVERS DO.)
use CGI::Carp qw/fatalsToBrowser/;
# Carp helps with debugging REMOVE IF Carp is not on your server, It should be.
$| = 1;
$title='title';
$dir='/tmp';
#This was the relative/absolute path to the directory you wish to upload to.
# ./ is the same directory as this script is placed. NEVER USE \
$limit=500;
# max xKb posts. Max file size in KB to upload.
@extens=qw(jpg gif bmp txt html htm pdf vso ppt xls doc);
# only upload files with the ext's in the brackets.
# ie @extens= qw(txt html htm asp this that);
$encoding='multipart/form-data';
$match=0;
$CGI::POST_MAX=1024 * $limit;
$q = new CGI;
print $q->header();
print $q->start_html(-title=>"$title\n",
-BGCOLOR=>'white',
-TEXT=>'navy',
-link=>'navy',
-vlink=>'red',
-alink=>'black');
print "\n";
print $q->startform($method,$action,$encoding);
print $q->h1("$title"), "Use this page to attach a file to an ATM issue.<br>This issue must already exist on ATM!<br><br>The file will be sent to '$FTP_SITE:/ftp/$FTP_DIR'<br><br>Enter your file to upload <font size=1>(@extens ${limit}Kb Max.)</font>\n";
($junk, $issue_id)=split(/=/, $ENV{'QUERY_STRING'});
print $q->filefield(-name=>'upload_file', -default=>'starting value', -size=>50, -maxlength=>180);
print $q->submit(-name=>'button_name', -value=>'UPLOAD');
print $q->endform;
...
open(OUTFILE, ">/tmp/$file") || &error ("Can't open /tmp/$file. $!");
binmode OUTFILE;
# binmode is for windows only. Ignored by unix
print "<b>Uploading document..please wait......";
while ($bytesread=read($filename,$buffer,1024))
{
#print "Printing to outfile\n";
print OUTFILE $buffer;
}
close (OUTFILE);
.....
make your mods to above.
Nedals
12-11-2005, 04:07 PM
<FORM ACTION="/bin/script_library/form_handler_mail" METHOD=post enctype="multipart/form-data" >
That does not look right... It would expect something like this
"/cgi-bin/script_library/form_handler_mail.pl" or .cgi
But maybe it's OK. Check with your host.