Sending attached files in php

Reply

Join Date: Jan 2007
Posts: 41
Reputation: 3DProf4online is an unknown quantity at this point 
Solved Threads: 1
3DProf4online 3DProf4online is offline Offline
Light Poster

Sending attached files in php

 
0
  #1
Sep 30th, 2008
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!
www.onestopmarketing.com - affordable SEO tools and services.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Sending attached files in php

 
0
  #2
Sep 30th, 2008
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.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Sending attached files in php

 
0
  #3
Oct 2nd, 2008
phpmailer is an amazing class and there are ready examples in the download, just do a search for phpmailer download.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,073
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Sending attached files in php

 
0
  #4
Oct 3rd, 2008
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 142
Reputation: mexabet is an unknown quantity at this point 
Solved Threads: 9
mexabet's Avatar
mexabet mexabet is offline Offline
Junior Poster

Re: Sending attached files in php

 
1
  #5
Oct 3rd, 2008
See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.
  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:
  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:
  1. $maxsize=28480; // Set the maximum upload size in bytes
You can edit the size to suit your needs.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: helpless_101 is an unknown quantity at this point 
Solved Threads: 0
helpless_101 helpless_101 is offline Offline
Newbie Poster

Re: Sending attached files in php

 
0
  #6
Oct 6th, 2008
Originally Posted by mexabet View Post
See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.
  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:
  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:
  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..
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 41
Reputation: 3DProf4online is an unknown quantity at this point 
Solved Threads: 1
3DProf4online 3DProf4online is offline Offline
Light Poster

Re: Sending attached files in php

 
0
  #7
Oct 13th, 2008
Thanks, mexabet, your post seems to be the most helpful for me!
www.onestopmarketing.com - affordable SEO tools and services.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Sending attached files in php

 
0
  #8
Oct 13th, 2008
Originally Posted by mexabet View Post
See if this helps. To use this script, you need to create a folder named uploads where the uploaded files would be stored.
  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:
  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:
  1. $maxsize=28480; // Set the maximum upload size in bytes
You can edit the size to suit your needs.
Originally Posted by 3DProf4online View Post
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.
  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.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 142
Reputation: mexabet is an unknown quantity at this point 
Solved Threads: 9
mexabet's Avatar
mexabet mexabet is offline Offline
Junior Poster

Re: Sending attached files in php

 
1
  #9
Oct 13th, 2008
R0bb0b,

No offenses taken! Instead, we are working together to get a good working script for our fellow community member.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Sending attached files in php

 
0
  #10
Oct 13th, 2008
Originally Posted by mexabet View Post
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.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC