Upload files or Images using PHP


/ Published in: PHP
Save to your folder(s)

simple php file uploader with check for file extension and size.


Copy this code and paste it in your HTML
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <?
  6. $notAllowedExts = array("exe", "rar", "com");
  7. $AllowedSize = (1 * 1024 * 1024); //=>bytes.
  8.  
  9. $temp = explode(".", $_FILES["file"]["name"]);
  10. $extension = end($temp);
  11. ?>
  12.  
  13. <h2>Uploaded File Info:</h2>
  14. <ul>
  15. <li>temp file: <?php echo $_FILES["file"]["tmp_name"]; ?>
  16. <li>Sent file: <?php echo $_FILES['file']['name']; ?>
  17. <li>File size: <?php echo $_FILES['file']['size']; ?> bytes
  18. <li>File type: <?php echo $_FILES['file']['type']; ?>
  19. <li>File Extension: <?php echo $extension; ?>
  20. </ul>
  21. <?
  22. if ($_FILES["file"]["size"] > $AllowedSize){
  23. echo '<font face="Tahoma" size="3" color="#F00000" ><b>The size of "'.
  24. $_FILES["file"]["name"] .'" file is more than '.
  25. ($AllowedSize / (1024*1024)).' Mb. </b></font>';
  26. return 0;
  27. }
  28.  
  29. if (in_array($extension, $notAllowedExts)) {
  30. echo '<font face="Tahoma" size="3" color="#F00000" ><b>"'.
  31. $_FILES["file"]["name"] .'" - not accepted file. </b></font>';
  32. return 0;
  33. }
  34.  
  35. if ($_FILES["file"]["error"] > 0){
  36. echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  37. return 0;
  38. }
  39.  
  40.  
  41. if (file_exists("t1/" . $_FILES["file"]["name"]))
  42. echo '<font face="Tahoma" size="3" color="#F00000" ><b>"'.
  43. $_FILES["file"]["name"] .'" - already exists. </b></font>';
  44. else{
  45. move_uploaded_file($_FILES["file"]["tmp_name"], "t1/" .
  46. $_FILES["file"]["name"]);
  47. echo '<font face="Tahoma" size="3" color="#008000" ><b>"'.
  48. $_FILES["file"]["name"].'" - was successfully uploaded.</b></font>';
  49. }
  50. ?>
  51. </body>
  52. </html>

URL: http://function-code.blogspot.com/2013/10/how-to-upload-files-or-images-using-php.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.