Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

email and upload

by stu96art (Scribe)
on Mar 17, 2003 at 18:57 UTC ( [id://243746]=perlquestion: print w/replies, xml ) Need Help??

stu96art has asked for the wisdom of the Perl Monks concerning the following question:

I have a HTML page that has form fields and a field to upload. I have been having trouble getting my files to upload. I searched for "uploads" and found a lot of good information, but for some reason I cannot get it to work eamil me anything. Here is my code for the cgi:
#!/usr/local/bin/perl -w use CGI; # load CGI routines use Mail::Sendmail; $q = new CGI; @names = $q->param; $name = $q->param('name'); $company = $q->param('company'); $address = $q->param('address'); $city = $q->param('city'); $state = $q->param('state'); $zip = $q->param('zip'); $phone = $q->param('phone'); $fax = $q->param('fax'); $email = $q->param('email'); $comments = $q->param('comments'); $file = $q->param('file'); print $q->header, # create the HTTP header $q->start_html(-title=>'Email Sent', -BACKGROUND=>'http://65.108.100.28/images/bkg3 +.jpg', -LINK=>'blue', -ALINK=>'maroon'), # start the HTML "<p align=\"center\"><font size=\"6\">Thank you<br><br></font +>", "<font size=\"7\">Your Email has been sent to stu96art@yahoo. +com</font>", # level 1 header '</p>', '<br><br><br><br>', "<p align=\"center\">Click here go to the <a color=\"blue\" h +ref=\"http://65.108.100.28/index.html\">home page</a><br><br>", "<p align=\"center\">Click here return to the <a color=\"blue +\" href=\"http://65.108.100.28/contact.html\">contact page</a><br><br +>", "<p align=\"center\">The values you sent were</p>", $q->h2("NAME: $name\n<br> COMPANY: $company\n<br> ADDRESS: $a +ddress\n<br> CITY: $city\n<br> STATE: $state\n<br> ZIP: $zip\n<br> PH +ONE: $phone\n<br> FAX: $fax\n<br> EMAIL: $email\n<br> COMMENTS: $comm +ents\n<br>"), $q->end_html; # end the HTML use MIME::Lite; $msg = new MIME::Lite From =>'technicu@technicut.net', To =>'stu96art@yahoo,com', Cc =>'some@other.com, some@more.com', smpt =>'64.176.60.59', Subject =>'A message with 2 parts...', Type =>'multipart/mixed'; attach $msg Type =>'TEXT', Data =>"NAME: $name\n COMPANY: $company\n ADDRESS: $add +ress\n CITY: $city\n STATE: $state\n ZIP: $zip\n PHONE: $phone\n FAX: + $fax\n EMAIL: $email\n COMMENTS: \n \n $comments"; attach $msg Type =>'image/gif', Path =>"$file", Filename =>"$file"; $msg->send;
I was also wondering how I could download any file, possible not just a .gif file. I would look to download .dxf, .cad (drawing files), but be able to upload any type of file. Is this possible? Thanks.

Replies are listed 'Best First'.
Re: email and upload
by Jenda (Abbot) on Mar 17, 2003 at 23:23 UTC

    If I understand you right you have a form containing something like:

    <input type="file" name="file">
    and want to mail the just uploaded file, right? It's not as simple as you do it. The $q->param('file'); does give you the name of the uploaded file, but if you tried -e $q->param('file'); you'd get false. Cause the file is of course NOT uploaded to the current directory, but to some temp directory. And since MIME::Lite expects a path to the file in it's Path parameter, the mailing fails. You have to use either
    ... Filename => $file, FH => $q->param('file'), ...
    (Not tested! At least I believe you can. I believe CGI.pm uses some magic so you can use the $q->param('file') as a filehandle.) Or
    # uploading the file... $filename = $query->param('file'); if ($filename ne ""){ $tmp_file = $query->tmpFileName($filename); } ... Filename => $filename, Path => $tmp_file, ...
    (Again not tested, but this is very similar to what I suggest in Mail::Sender's docs.)

    HTH, Jenda

      This is almost correct....the value of $query->param('file') is a filehandle to the file to be uploaded, which in scalar context yields the name of the file. From my limited experience (under SpeedyCGI on Linux), the file isn't even uploaded at the point you do that, but only when you try to read from the filehandle.

      From the CGI manpage:

      When the form is processed, you can retrieve the entered filename by calling param():
      $filename = $query->param('uploaded_file');

      Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

      The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls:

      # Read a text file and print it out while (<$filename>) { print; } # Copy a binary file to somewhere safe open (OUTFILE,">>/usr/local/web/users/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; }

      However, there are problems with the dual nature of the upload fields. If you use strict, then Perl will complain when you try to use a string as a filehandle. You can get around this by placing the file reading code in a block containing the no strict pragma. More seriously, it is possible for the remote user to type garbage into the upload field, in which case what you get from param() is not a filehandle at all, but a string.

      To be safe, use the upload() function (new in version 2.47). When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle.

      $fh = $query->upload('uploaded_file'); while (<$fh>) { print; }

      In an array context, upload() will return an array of filehandles. This makes it possible to create forms that use the same name for multiple upload fields.

      This is the recommended idiom.

      When a file is uploaded the browser usually sends along some information along with it in the format of headers. The information usually includes the MIME content type. Future browsers may send other information as well (such as modification date and size). To retrieve this information, call uploadInfo(). It returns a reference to an associative array containing all the document headers.

      $filename = $query->param('uploaded_file'); $type = $query->uploadInfo($filename)->{'Content-Type'}; unless ($type eq 'text/html') { die "HTML FILES ONLY!"; }

      If you are using a machine that recognizes "text" and "binary" data modes, be sure to understand when and how to use them (see the Camel book). Otherwise you may find that binary files are corrupted during file uploads.

      Sorry for the long excerpt.

      --roundboy

Re: email and upload
by thatguy (Parson) on Mar 17, 2003 at 19:44 UTC
    Do you get an error in your webserver's error_log?  There's some good information to be had in error_logs along with the ever usefull 'use CGI::Carp qw(fatalsToBrowser);'

    -phill

      I apologize, but I do not thinkn that I get an error in my servers log. I look at the "access-log" and I see no errors. I do not know where else to look for an error log. I could not find one through my looking.
Re: email and upload
by Mr. Muskrat (Canon) on Mar 17, 2003 at 21:36 UTC

    Switch it around a bit...

    use MIME::Lite; my $msg = new MIME::Lite( From =>'technicu@technicut.net', To =>'stu96art@yahoo.com', Cc =>'some@other.com, some@more.com', smpt =>'64.176.60.59', Subject =>'A message with 2 parts...', Type =>'multipart/mixed'); $msg->attach(Type => 'TEXT', Data => "NAME: $name\nCOMPANY: $company\nADDRESS: $ad +dress\nCITY: $city\nSTATE: $state\nZIP: $zip\nPHONE: $phone\nFAX: $fa +x\nEMAIL: $email\nCOMMENTS: $comments\n\n"); $msg->attach(Type =>'image/gif', Path => $file);

    This is untested and should be treated accordingly.

    Updated: Added some code that was missing :o

    2nd Update: fixed typo in To address. Also, are you sure that the Cc addresses are valid? :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://243746]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found