Solved

Attaching a user file to a submitted form

Posted on 2000-03-31
9
236 Views
Last Modified: 2013-12-25
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
Comment
Question by:Benny00
  • 6
  • 3
9 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 2673143
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
 

Author Comment

by:Benny00
ID: 2673167
I am using sendmail

Is there a better way?
0
 
LVL 10

Expert Comment

by:makerp
ID: 2673192
# 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
 
LVL 10

Expert Comment

by:makerp
ID: 2673204
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:makerp
ID: 2673209
any way good luck .. im off home

any questions and ill awnser on sunday
0
 

Author Comment

by:Benny00
ID: 2674431
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
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 2679214
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
 

Author Comment

by:Benny00
ID: 2680534
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
 
LVL 10

Expert Comment

by:makerp
ID: 2682902
just add a path before the $save_name
open(OUTFILE,">/usr/temp/blah/".$save_name) or die("cannt copy image over ! ... ".$!);
     
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now