Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Attaching a user file to a submitted form in Perl

I asked a question previously about how to attach a file to a user-specified e-mail in perl.  this is the answer in code that I was given.  it does not seem to give me the correct file.  This is the error I get...

[03/Apr/2000:13:42:10] 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/accomdev/cg
i-bin/mail-attach.cgi line 51.")          

The filename should show up in this error message, but it does not which leads me to believe that it doesn't have a correct file.  Can someone please help me???

#!/usr/local/bin/perl
#
# PROGRAM NAME:
#
#
# DESCRIPTION:
#
# 04-03-2000
#
################################################################

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=mail-attach.cgi 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
{
      print $query->header();

     # 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,">/tmp/".$save_name) or die("cannt copy image over ! ... $save_name".$!);
     # 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 => "benjamin.t.novak\@ac.com",
                  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
Benny00
Asked:
Benny00
  • 7
  • 6
  • 4
1 Solution
 
maneshrCommented:
you are getting that error because the foll open is failing...

open(OUTFILE,">/tmp/".$save_name) or die("cannt copy image over ! ... $save_name".$!);


the reason why open is failing could be because there is nothing in $save_name. From the code above $save_name gets its value from @im array ($save_name = @im[$#im];).

where is @im array defined & populated??

Add the foll. 3 lines .....

print "Content-type: text/html\n\n";
print "/tmp/",$save_name;
exit;

BEFORE .............
open(OUTFILE,">/tmp/".$save_name) or die("cannt copy image over ! ... $save_name".$!);


And see that is shown in your browser. Then see if you have the proper permissions to that file/directory.

Also change the line

$save_name = @im[$#im];

TO....

$save_name = $im[$#im];

and try the same print statements.


Hope that helps
0
 
Benny00Author Commented:
Yes, there is nothing in that $save_name variable...I did know that before hand because of the way the error msg reads.

My real question was how do I get that to be the correct file and make all this work?
0
 
maneshrCommented:

the answer lies in ..

#get the last item from the array
$save_name = @im[$#im];


where is @im array defined. have you attached the complete code ??
unless one knows where @im comes from, one cant give you the right answer.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Benny00Author Commented:
I received this code from someone...they told me this would work...that is the complete code...I accidently accepted the answer before I got to try it out...

So apparently @im comes from nowhere...Any ideas on how this should work?  I am pretty new with Perl as you can tell
0
 
maneshrCommented:
for the sake of testing, try this.

change the line

                    $save_name = @im[$#im];

                    TO....

                    $save_name = "testing";

i looked at your code and it seems pretty simple. i would like to know what it is that you wanted to achieve when you sought this script.
That would help me give a much better and working solution to you.

0
 
Benny00Author Commented:
My goal is to let the user fill out a form, and specify a file that they would like to attach with their form data.  Hopefully then the file could be uploaded to the server into a temporary directory and then attached to the e-mail with the form data, and then removed off of the server.  Thanks, in advance.
0
 
maneshrCommented:
"My goal is to let the user fill out a form, and specify a file that they would like to attach with their form data.  Hopefully then the file could be uploaded to the server into a temporary directory and then attached to the e-mail with the form data, .."


now when the email is sent what name should be given to the file. should the file have its original name?? or should is have some new/temporary name?


".....and then removed off of the server.  "

the above is NOT implemented by the code you have sent & hence is an additional requirements.

i would suggest that you tackle the first before going to the 2nd. Also you have not told me what happens when you use...
$save_name = "testing";

Rgds
0
 
makerpCommented:
# sorry @im should be @parts
@parts = split /(\\)|(\/)/,$filename;
# get the last item from the array
$save_name = @parts[$#parts];

one other thing :    
make sure you have write permissions on the dir you are trying to create the file in. on NT the web server runs under the ISUR_<machine_name> account .. THIS account needs write access. ask your system admin. they will know what you mean if you say your web server needs write access on the dir

if you want to create the file in an other dir put a path before $save_name

open(OUTFILE,">/usr/blah/tmp/".$save_name) or die("cannt copy image over ! ... $save_name".$!);

once you have used the file do unlink($save_name) to remove the file. you will need delet access for this (ask system admin). remeber to pre-pend the path if you specified one when you opened it
     
0
 
makerpCommented:
are sorted out now Benny00 ???

maneshr : im supprised that you didnt spot the fault !!

# sorry @im should be @parts
@parts = split /(\\)|(\/)/,$filename;
# get the last item from the array
$save_name = @parts[$#parts];

:)

0
 
Benny00Author Commented:
Looks good thanks...

Just have to figure out how to attach a non-specific file type to an e-mail...or if I have to write a bunch of if statements for the "type" of attachment...Any ideas?
0
 
makerpCommented:
my sendmail expericence is ZERO

sorry ....
0
 
maneshrCommented:
makerp ,


maneshr : im supprised that you didnt spot the fault !!

whay should you be surprise at all?? :). Anyway i am happy that the problem was solved for Benny00
0
 
makerpCommented:
your on the top experts board 12000 or so points

im pretty new to perl myself but im finding it to be okay
0
 
maneshrCommented:
"your on the top experts board 12000 or so points

im pretty new to perl myself but im finding it to be okay"

*laughs* you sound very much like my ex-boss who belived that only people with Master in Computer Science could be excellent software developers.

i am very happy to know that you were able to solve the problem every though you are new to it. i am sure you will be in that list someday soon!!

wish you all the best.

Rgds
0
 
makerpCommented:
most of the best software heads that ive meet have never been to college


anyway

cheers mate

:)
0
 
maneshrCommented:
"most of the best software heads that ive meet have never been to college"


you betcha!!

Take care

0
 
makerpCommented:
bye bye
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.

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