• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Attaching a user file to a submitted form

I have been working on a web page in which I need to submit a form and be able to have the user attach a file to that form and e-mail the form results and attachment to another source.  What kind of HTML commands do I need to use and more importantly, how do I attach a user specified file in Perl to that form.  I can already send the form results, I just need to figure out the file attachment part.  Thanks

Benny00
0
Benny00
Asked:
Benny00
  • 6
  • 3
1 Solution
 
makerpCommented:
what mail package are you using??

waht you will have to do is provide the user with a file upload inout box on the form and your script will have to recive this file and save it to a temp file then attach it to the mail.

i have the code for the upload of the file in perl...

0
 
Benny00Author Commented:
I am using sendmail

Is there a better way?
0
 
makerpCommented:
# use the cgi package
use CGI;
# create a cgi object
$query = new CGI;

# if we have no parameters then display the form
if(!query->param())
{
     print $query->header();
     print("
          <FORM ACTION=upload_test.pl METHOD=POST ENCTYPE=multipart/form-data>
               Enter your name and select a file for upload :
               <INPUT TYPE=TEXT NAME=name>
               <INPUT TYPE=FILE NAME=file>
               <BR>
               <INPUT TYPE=SUBMIT VALUE=upload>
          </FORM>
     ");
}
else
{
     # get the name the user entered on the form
     $name = $query->param('name')
     # get the filename (this is also a filehandle as will be demonstated shortly)
     $filename = $query->param('file');

     # spilt it so we can get the filename and extension
     @parts = split /(\\)|(\/)/,$filename;
     # get the last item from the array
     $save_name = @im[$#im];
     # open the file (make sure the web server account has write permissions on the directory)
     open(OUTFILE,">".$save_name) or die("cannt copy image over ! ... ".$!);
     # only set binmode if you are sure a binary file is coming over
     binmode OUTFILE;
     # use the filehandle $filename read it in and write it out to our OUTFILE
     while ($bytesread = read($filename,$buffer,1024))
     {
          print OUTFILE $buffer;
     }

     # now create you mail message as you would but add the attachment
     # use the $save_name because this is the name the file would have been saved
     # as in the dir. this example will save the file to the same dir as the script
     # is in
}
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
makerpCommented:
i dont know how to do attachments in sendmail.

but ive done the difficult bit for you. the attaching will be one line of code...

make sure you have write permissions for the web user account (speck to your system admin) on the dir you want to write to. also delete that file after if you have nop need for if (use unlink($save_name))
0
 
makerpCommented:
any way good luck .. im off home

any questions and ill awnser on sunday
0
 
Benny00Author Commented:
I'm having a problem...

This is the error I get...

[31/Mar/2000:13:19:52] failure (21045): for host 10.10.43.228 trying to GET /cgi
-bin/mail-attach.cgi, cgieng_scan_headers reports: the CGI program /opt/docs/acc
omdev/cgi-bin/mail-attach.cgi did not produce a valid header (name without value
: got line "can't locate object method "param" via package "query" at /opt/docs/
accomdev/cgi-bin/mail-attach.cgi line 28.")      

This is my code...

#!/usr/local/bin/perl
#
# Copyright 1996-1998 Andersen Consulting
#
# PROGRAM NAME:
# asg_ugrad_web2k.cgi
#
# DESCRIPTION:
# Validates the form on asg_ugrad_web2k_apply.html.  Information is
# sent to Marcus Schaper.
#
# Created by Anthony W. Chang
# 05-14-99
#
################################################################

require ("cgi-lib.pl");
# use the cgi package
use CGI;
use MIME::Entity;

# create a cgi object
$query = new CGI;

# if we have no parameters then display the form
if(!query->param())  
{
     print $query->header();
     print("
          <FORM ACTION=upload_test.pl METHOD=POST ENCTYPE=multipart/form-data>
               Enter your name and select a file for upload :
               <INPUT TYPE=TEXT NAME=name>
               <INPUT TYPE=FILE NAME=file>
               <BR>
               <INPUT TYPE=SUBMIT VALUE=upload>
          </FORM>
     ");
}
else
{
     # get the name the user entered on the form
     $name = $query->param('name');
     # get the filename (this is also a filehandle as will be demonstated shortly)
     $filename = $query->param('file');

     # spilt it so we can get the filename and extension
     @parts = split /(\\)|(\/)/,$filename;
     # get the last item from the array
     $save_name = @im[$#im];
     # open the file (make sure the web server account has write permissions on the directory)
     open(OUTFILE,">".$save_name) or die("cannt copy image over ! ... ".$!);
     # only set binmode if you are sure a binary file is coming over
     binmode OUTFILE;
     # use the filehandle $filename read it in and write it out to our OUTFILE
     while ($bytesread = read($filename,$buffer,1024))
     {
          print OUTFILE $buffer;
     }

     # now create you mail message as you would but add the attachment
     # use the $save_name because this is the name the file would have been saved
     # as in the dir. this example will save the file to the same dir as the script
     # is in
       $top = build MIME::Entity From => $from,
                   To => "play2win\@mediaone.net",
                  Subject => "Test";      
       $top->attach( Path => $save_name,
                   Type => "text/html",
                  Encoding => "-SUGGEST"
                  );
      
       open(MAIL,"|/usr/bin/sendmail -t -oi");
       $top->print(\*MAIL);
       close MAIL;
                  
}

0
 
makerpCommented:
lose the require cgi-lib stmt..if you dont need it

if(!$query->param())  
{

the $ was missing from your ref to the cgi object..
     
0
 
Benny00Author Commented:
I keep on getting this error...

[03/Apr/2000:10:06:36] failure ( 1186): for host 10.10.43.228 trying to POST /cg
i-bin/mail-attach.cgi, cgieng_scan_headers reports: the CGI program /opt/docs/ac
comdev/cgi-bin/mail-attach.cgi did not produce a valid header (name without valu
e: got line "cannt copy image over ! ... is a directory at /opt/docs/accomd
ev/cgi-bin/mail-attach.cgi line 51.")

How do I set the directory for where I want the tempororily uploaded file to be?
0
 
makerpCommented:
just add a path before the $save_name
open(OUTFILE,">/usr/temp/blah/".$save_name) or die("cannt copy image over ! ... ".$!);
     
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now