943,743 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3978
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 30th, 2008
0

Sending attached files in php

Expand Post »
Could you please help realize the procedure of sending attached files in php. If it is possible, please, provide me with ready code examples. Thanks in advance!
Reputation Points: 10
Solved Threads: 1
Light Poster
3DProf4online is offline Offline
41 posts
since Jan 2007
Sep 30th, 2008
0

Re: Sending attached files in php

There are multiple resources online which deal with this issue, a quick search comes up with exactly what you are looking for, please research and dont ask other people to do all the work.

Post an example of what you have if it isnt working and we can assist you.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Oct 2nd, 2008
0

Re: Sending attached files in php

phpmailer is an amazing class and there are ready examples in the download, just do a search for phpmailer download.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 3rd, 2008
0

Re: Sending attached files in php

Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 3rd, 2008
1

Re: Sending attached files in php

See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $maxsize=28480; // Set the maximum upload size in bytes
  3. if (!$_POST['submit']) {
  4. //print_r($_FILES);
  5. $error=" ";
  6. // This will cause the rest of the process to be skipped
  7. //and the upload form displays
  8. }
  9. if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
  10. !isset($error)) {
  11. $error = "<b>You must upload a file!</b><br /><br />";
  12. unlink($_FILES['upload_file']['tmp_name']);
  13. }
  14. if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
  15. $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
  16. unlink($_FILES['upload_file']['tmp_name']);
  17. }
  18. if($_FILES['upload_file']['type'] != "image/gif" AND
  19. $_FILES['upload_file']['type'] != "image/pjpeg" AND
  20. $_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
  21. $error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
  22. unlink($_FILES['upload_file']['tmp_name']);
  23. }
  24. if (!isset($error)) {
  25. move_uploaded_file($_FILES['upload_file']['tmp_name'],
  26. "uploads/".$_FILES['upload_file']['name']);
  27. print "Thank you for your upload.";
  28. exit;
  29. }
  30. else
  31. {
  32. echo ("$error");
  33. }
  34. ?>
  35.  
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  37. <html xmlns="http://www.w3.org/1999/xhtml">
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  40. <title>PHP File Upload Script</title>
  41. </head>
  42.  
  43. <body>
  44. <form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
  45. enctype="multipart/form-data">
  46. Choose a file to upload:<br />
  47. <input type="file" name="upload_file" size="50" />
  48. <br />
  49. <input type="submit" name="submit" value="Submit" />
  50. <input type="reset" name="Reset" value="Reset" />
  51. </form>
  52. </body>
  53. </html>

This bit of code moves the uploaded file from a temporary directory into the uploads directory:
PHP Syntax (Toggle Plain Text)
  1. move_uploaded_file($_FILES['upload_file']['tmp_name'],
  2. "uploads/".$_FILES['upload_file']['name']);

The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:
PHP Syntax (Toggle Plain Text)
  1. $maxsize=28480; // Set the maximum upload size in bytes
You can edit the size to suit your needs.
Reputation Points: 16
Solved Threads: 9
Junior Poster
mexabet is offline Offline
148 posts
since Mar 2008
Oct 6th, 2008
0

Re: Sending attached files in php

Click to Expand / Collapse  Quote originally posted by mexabet ...
See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $maxsize=28480; // Set the maximum upload size in bytes
  3. if (!$_POST['submit']) {
  4. //print_r($_FILES);
  5. $error=" ";
  6. // This will cause the rest of the process to be skipped
  7. //and the upload form displays
  8. }
  9. if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
  10. !isset($error)) {
  11. $error = "<b>You must upload a file!</b><br /><br />";
  12. unlink($_FILES['upload_file']['tmp_name']);
  13. }
  14. if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
  15. $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
  16. unlink($_FILES['upload_file']['tmp_name']);
  17. }
  18. if($_FILES['upload_file']['type'] != "image/gif" AND
  19. $_FILES['upload_file']['type'] != "image/pjpeg" AND
  20. $_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
  21. $error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
  22. unlink($_FILES['upload_file']['tmp_name']);
  23. }
  24. if (!isset($error)) {
  25. move_uploaded_file($_FILES['upload_file']['tmp_name'],
  26. "uploads/".$_FILES['upload_file']['name']);
  27. print "Thank you for your upload.";
  28. exit;
  29. }
  30. else
  31. {
  32. echo ("$error");
  33. }
  34. ?>
  35.  
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  37. <html xmlns="http://www.w3.org/1999/xhtml">
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  40. <title>PHP File Upload Script</title>
  41. </head>
  42.  
  43. <body>
  44. <form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
  45. enctype="multipart/form-data">
  46. Choose a file to upload:<br />
  47. <input type="file" name="upload_file" size="50" />
  48. <br />
  49. <input type="submit" name="submit" value="Submit" />
  50. <input type="reset" name="Reset" value="Reset" />
  51. </form>
  52. </body>
  53. </html>

This bit of code moves the uploaded file from a temporary directory into the uploads directory:
PHP Syntax (Toggle Plain Text)
  1. move_uploaded_file($_FILES['upload_file']['tmp_name'],
  2. "uploads/".$_FILES['upload_file']['name']);

The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:
PHP Syntax (Toggle Plain Text)
  1. $maxsize=28480; // Set the maximum upload size in bytes
You can edit the size to suit your needs.


hi.. does this code displays the uploaded picture? tnx..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
helpless_101 is offline Offline
5 posts
since Oct 2008
Oct 13th, 2008
0

Re: Sending attached files in php

Thanks, mexabet, your post seems to be the most helpful for me!
Reputation Points: 10
Solved Threads: 1
Light Poster
3DProf4online is offline Offline
41 posts
since Jan 2007
Oct 13th, 2008
0

Re: Sending attached files in php

Click to Expand / Collapse  Quote originally posted by mexabet ...
See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $maxsize=28480; // Set the maximum upload size in bytes
  3. if (!$_POST['submit']) {
  4. //print_r($_FILES);
  5. $error=" ";
  6. // This will cause the rest of the process to be skipped
  7. //and the upload form displays
  8. }
  9. if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
  10. !isset($error)) {
  11. $error = "<b>You must upload a file!</b><br /><br />";
  12. unlink($_FILES['upload_file']['tmp_name']);
  13. }
  14. if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
  15. $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
  16. unlink($_FILES['upload_file']['tmp_name']);
  17. }
  18. if($_FILES['upload_file']['type'] != "image/gif" AND
  19. $_FILES['upload_file']['type'] != "image/pjpeg" AND
  20. $_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
  21. $error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
  22. unlink($_FILES['upload_file']['tmp_name']);
  23. }
  24. if (!isset($error)) {
  25. move_uploaded_file($_FILES['upload_file']['tmp_name'],
  26. "uploads/".$_FILES['upload_file']['name']);
  27. print "Thank you for your upload.";
  28. exit;
  29. }
  30. else
  31. {
  32. echo ("$error");
  33. }
  34. ?>
  35.  
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  37. <html xmlns="http://www.w3.org/1999/xhtml">
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  40. <title>PHP File Upload Script</title>
  41. </head>
  42.  
  43. <body>
  44. <form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post"
  45. enctype="multipart/form-data">
  46. Choose a file to upload:<br />
  47. <input type="file" name="upload_file" size="50" />
  48. <br />
  49. <input type="submit" name="submit" value="Submit" />
  50. <input type="reset" name="Reset" value="Reset" />
  51. </form>
  52. </body>
  53. </html>

This bit of code moves the uploaded file from a temporary directory into the uploads directory:
PHP Syntax (Toggle Plain Text)
  1. move_uploaded_file($_FILES['upload_file']['tmp_name'],
  2. "uploads/".$_FILES['upload_file']['name']);

The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:
PHP Syntax (Toggle Plain Text)
  1. $maxsize=28480; // Set the maximum upload size in bytes
You can edit the size to suit your needs.
Thanks, mexabet, your post seems to be the most helpful for me!
Sorry to rain on your parade, but you should never go by $_FILES['upload_file']['type'] as a security check since this is just a line in the header and can easily be reproduced manually. The browser doesn't even have to provide this info to php so sometimes you won't even be given this info. I could very easily send a php file with the header of "image/gif" or if this is a mail script I could send a virus or what ever I wanted to. You should always parse $_FILES['upload_file']['name'] and check the extension that way. This is how I would go about verifying the extension of the filename.
php Syntax (Toggle Plain Text)
  1. <?php
  2. function returnFileName($file)
  3. {
  4. $dot = strrpos($file, '.');
  5. if($dot === false)//file has no dot
  6. {
  7. return false;
  8. }
  9.  
  10. $fileinfo = array();
  11. $fileinfo['base'] = substr($file, 0, $dot);
  12. $fileinfo['ext'] = strtolower(substr($file, $dot + 1));
  13. return $fileinfo;
  14. }
  15.  
  16. $allowedext = array("jpg", "gif");
  17. $filearray = returnFileName(basename($_FILES['upload_file']['name']));
  18.  
  19. if($filearray == false || !isset($filearray['ext']) || !in_array($filearray['ext'], $allowedext))
  20. {
  21. $error .= "invalid file type";
  22. }
  23. ?>
Last edited by R0bb0b; Oct 13th, 2008 at 8:33 am.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 13th, 2008
1

Re: Sending attached files in php

R0bb0b,

No offenses taken! Instead, we are working together to get a good working script for our fellow community member.
Reputation Points: 16
Solved Threads: 9
Junior Poster
mexabet is offline Offline
148 posts
since Mar 2008
Oct 13th, 2008
0

Re: Sending attached files in php

Click to Expand / Collapse  Quote originally posted by mexabet ...
R0bb0b,

No offenses taken! Instead, we are working together to get a good working script for our fellow community member.
And the opportunity to bring into the open and alert countless others of a security misconception that apparently often goes unnoticed.
Last edited by R0bb0b; Oct 13th, 2008 at 10:59 am.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: mysql_real_escape_string ?
Next Thread in PHP Forum Timeline: Drupal 404 Error Page Not Working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC