Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Attaching a user file to a submitted form

Posted on 2000-03-31
9
Medium Priority
?
244 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

636 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