Solved

Attaching a user file to a submitted form

Posted on 2000-03-31
9
238 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update a registry value to every user hive 15 81
Powershell variable with -SearchBase cmdlet 2 115
Python variable _ manually assigned 9 81
Recursively Delete Files 5 80
Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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