Solved

Attaching a user file to a submitted form in Perl

Posted on 2000-04-03
17
215 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

631 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