Solved

Attaching a user file to a submitted form in Perl

Posted on 2000-04-03
17
209 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

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

Suggested Solutions

Title # Comments Views Activity
Need to convert a simple VB script to PowerShell 3 85
excel file 5 76
User profile Size Report 3 84
Quickest way to query Windows Event ID from a Linux Device 3 53
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
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.
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…
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…

808 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