Solved

Attaching a user file to a submitted form in Perl

Posted on 2000-04-03
17
206 Views
Last Modified: 2013-12-25
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
Comment
Question by:Benny00
  • 7
  • 6
  • 4
17 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2681317
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
 

Author Comment

by:Benny00
ID: 2681362
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2681417

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
 

Author Comment

by:Benny00
ID: 2681437
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2681581
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
 

Author Comment

by:Benny00
ID: 2681596
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2681698
"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
 
LVL 10

Accepted Solution

by:
makerp earned 100 total points
ID: 2682952
# 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 10

Expert Comment

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

Author Comment

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

Expert Comment

by:makerp
ID: 2683864
my sendmail expericence is ZERO

sorry ....
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2683979
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
 
LVL 10

Expert Comment

by:makerp
ID: 2683989
your on the top experts board 12000 or so points

im pretty new to perl myself but im finding it to be okay
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2684033
"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
 
LVL 10

Expert Comment

by:makerp
ID: 2684120
most of the best software heads that ive meet have never been to college


anyway

cheers mate

:)
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2684150
"most of the best software heads that ive meet have never been to college"


you betcha!!

Take care

0
 
LVL 10

Expert Comment

by:makerp
ID: 2684185
bye bye
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

707 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

12 Experts available now in Live!

Get 1:1 Help Now