Guest User

Untitled

a guest
May 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <?php
  2. //define a maxim size for the uploaded images in Kb
  3. define ("MAX_SIZE","100");
  4.  
  5. //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
  6. function getExtension($str) {
  7. $i = strrpos($str,".");
  8. if (!$i) { return ""; }
  9. $l = strlen($str) - $i;
  10. $ext = substr($str,$i+1,$l);
  11. return $ext;
  12. }
  13.  
  14. //This variable is used as a flag. The value is initialized with 0 (meaning no error found)
  15. //and it will be changed to 1 if an errro occures.
  16. //If the error occures the file will not be uploaded.
  17. $errors=0;
  18. //checks if the form has been submitted
  19. if(isset($_POST['Submit']))
  20. {
  21. //reads the name of the file the user submitted for uploading
  22. $image=$_FILES['image']['name'];
  23. //if it is not empty
  24. if ($image)
  25. {
  26. //get the original name of the file from the clients machine
  27. $filename = stripslashes($_FILES['image']['name']);
  28. //get the extension of the file in a lower case format
  29. $extension = getExtension($filename);
  30. $extension = strtolower($extension);
  31. //if it is not a known extension, we will suppose it is an error and will not upload the file,
  32. //otherwise we will do more tests
  33. if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  34. {
  35. //print error message
  36. echo '<h1>Unknown extension!</h1>';
  37. $errors=1;
  38. }
  39. else
  40. {
  41. //get the size of the image in bytes
  42. //$_FILES['image']['tmp_name'] is the temporary filename of the file
  43. //in which the uploaded file was stored on the server
  44. $size=filesize($_FILES['image']['tmp_name']);
  45.  
  46. //compare the size with the maxim size we defined and print error if bigger
  47. if ($size > MAX_SIZE*1024)
  48. {
  49. echo '<h1>You have exceeded the size limit!</h1>';
  50. $errors=1;
  51. }
  52.  
  53.  
  54. $uploaddir = '/upload';//<----This is all I changed
  55. mkdir( $uploaddir , 0777 ) ;
  56. $uploadfile = $uploaddir . basename($_FILES['image']['name']);
  57.  
  58. echo '<pre>';
  59. chmod($_FILES['image']['tmp_name'], 0644);
  60. if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
  61. echo "File is valid, and was successfully uploaded.\n";
  62. } else {
  63. echo "Possible file upload attack!\n";
  64. }
  65. if (!$copied)
  66. {
  67. echo '<h1>Copy unsuccessfull!</h1>';
  68. $errors=1;
  69. }}}}
  70.  
  71. //If no errors registred, print the success message
  72. if(isset($_POST['Submit']) && !$errors)
  73. {
  74. echo "<h1>File Uploaded Successfully! Try again!</h1>";
  75. }
  76.  
  77. ?>
Add Comment
Please, Sign In to add comment