I have created two scripts: a form and a form handler. The idea is to send attachments with email. However, I get the following error messages (associated with the form handler) when I submit the form.
Warning: fread(): supplied argument is not a valid stream resource in /home/lhsnycne/public_html/LHS/contact/sendmixed.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in /home/lhsnycne/public_html/LHS/contact/sendmixed.php on line 44
Here is the form:
1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2. <html>
3. <head>
4. <title>Attachment Form</title>
5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6. </head>
7.
8. <body>
9. <form action="sendmixed.php" method="post"
10 enctype="multipart/form-data">
11. <table>
12. <tr><td>To:</td>
13. <td><input type="text" name="to" size="40"/></td>
14. </tr>
15.
16. <tr><td>From:</td>
17. <td><input type="text" name="from" size="40"/></td>
18. </tr>
19.
20. <tr><td>Subject:</td>
21. <td><input type="text" name="re" size="40"/></td>
22. </tr>
23.
24. <tr><td>Message:</td>
25. <td>
26. <textarea cols="30" rows="5" name="comments">
27. </textarea>
28. </td>
29. </tr>
30.
31. <tr><td>Attachment:</td>
32. <td><input type="file" name="att" size="26" /></td>
33. </tr><td colspan="2">
34. <input type="submit" value="Send Form" />
35. </td>
36. </tr>
37. </table>
38. </form>
39. </body>
40. </html>
And here is the formhadler:
1. <?php
2.
3. #variables passed are $to,$from,$from,$re,$comments,$att
4.
5. #open the file
6. $fp = fopen( $att_name, "r");
7.
8. #read the file into a variable
9. $file = fread( $fp, $att_size );
10.
11. #encode the data for safe transit
12. #and insert \r\n every 76-characters
13. $file = chunk_split(base64_encode($file));
14.
15. #get a random 32-character hexadecimal number
16. $num = md5( time() );
17.
18. #define the main headers
19. $hdr = "From:$from\r\n";
20. $hdr .= "MIME-Version: 1.0\r\n";
21. $hdr .= "Content-Type: multipart/mixed; ";
22. $hdr .= "boundary=$num\r\n";
23. $hdr .= "--$num\r\n"; #start boundary here
24.
25. #define the message section
26. $hdr .= "Content-Type: text/plain\r\n";
27. $hdr .= "Content-Transfer-Encoding: 8bit\r\n\n";
28 $hdr .= "$comments\r\n";
29. $hdr .= "--$num\n"; #start boundary here
30.
31. #define the attachment section
32. $hdr .= "Content-Type: $att_type; ";
33. $hdr .= "name=\"$att_name\"\r\n";
34. $hdr .= "Content-Transfer-Encoding: base64\r\n";
35. $hdr .= "Content-Disposition: attachment; ";
36. $hdr .= "filename=\"$att_name\"\r\n\n";
37. $hdr .= "$file\r\n";
38. $hdr .= "--$num--"; #final boundary here
39.
40. #send the mail now
41. mail( $to, $re, "", $hdr);
42.
43. #close the file
44. fclose($file);
45. ?>
Please help.