?
Solved

Attaching a user file to a submitted form

Posted on 2000-03-31
9
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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