upload.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <?php
  11. const MAX_FILE_SIZE = 5000000;
  12. const IMAGE_DIR = 'i';
  13. if (!is_dir(IMAGE_DIR)) {
  14. mkdir(IMAGE_DIR, 0777, true);
  15. return;
  16. }
  17. function verbose_error() {
  18. echo '<code style="white-space: pre;">';
  19. $inipath = php_ini_loaded_file();
  20. if ($inipath) {
  21. echo 'Loaded php.ini: ' . $inipath . '<br>';
  22. } else {
  23. echo 'A php.ini file is not loaded<br>';
  24. }
  25. echo 'upload_max_filesize: ' . ini_get('upload_max_filesize') . '<br>';
  26. echo 'post_max_size: ' . ini_get('post_max_size') . '<br>';
  27. print_r($_FILES['file']);
  28. echo '</code>';
  29. }
  30. function dbg($msg) {
  31. echo "<div>[DEBUG] $msg</div>";
  32. }
  33. // returns 1 on ok
  34. // returns 0 on error
  35. function check_if_valid_image() {
  36. if(isset($_POST["submit"])) {
  37. $tmp_img_name = $_FILES['file']["tmp_name"];
  38. if (!$tmp_img_name) {
  39. dbg('$_FILES:');
  40. print_r($_FILES);
  41. dbg("warning file tmp_name not found!");
  42. dbg("using 'name' instead ...");
  43. $tmp_img_name = $_FILES['file']["name"];
  44. if(!$tmp_img_name) {
  45. dbg("Error: name not found either");
  46. return 0;
  47. }
  48. }
  49. $check = getimagesize($tmp_img_name);
  50. if($check !== false) {
  51. dbg("File is an image - " . $check["mime"] . ".");
  52. return 1;
  53. } else {
  54. dbg("File is not an image.");
  55. return 0;
  56. }
  57. dbg("tmp name: " . $tmp_img_name);
  58. } else {
  59. dbg("submit is empty");
  60. }
  61. return 1;
  62. }
  63. function upload_image() {
  64. if (!$_FILES['file']) {
  65. return;
  66. }
  67. $target_file = IMAGE_DIR . '/' . basename($_FILES['file']["name"]);
  68. $uploadOk = 1;
  69. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  70. // Check if image file is a actual image or fake image
  71. $uploadOk = check_if_valid_image();
  72. // Check if file already exists
  73. if (file_exists($target_file)) {
  74. echo "Sorry, file already exists.";
  75. $uploadOk = 0;
  76. }
  77. // Check file size
  78. if ($_FILES['file']["size"] > MAX_FILE_SIZE) {
  79. echo "Sorry, your file is too large. (" . $_FILES['file']["size"] . "/" . MAX_FILE_SIZE . ")";
  80. $uploadOk = 0;
  81. }
  82. dbg("file size: " . $_FILES['file']["size"]);
  83. // Allow certain file formats
  84. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  85. && $imageFileType != "gif" ) {
  86. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  87. $uploadOk = 0;
  88. }
  89. dbg("file type: " . $imageFileType);
  90. // Check if $uploadOk is set to 0 by an error
  91. if ($uploadOk == 0) {
  92. echo 'Sorry, your file was not uploaded.<br>';
  93. verbose_error();
  94. // if everything is ok, try to upload file
  95. } else {
  96. if (move_uploaded_file($_FILES['file']["tmp_name"], $target_file)) {
  97. $fileName = basename($_FILES['file']["name"]);
  98. $fileName = htmlspecialchars($fileName);
  99. echo 'The file <a href="/' . IMAGE_DIR . '/' . $fileName . '">' . htmlspecialchars($fileName) . '</a> has been uploaded.';
  100. } else {
  101. echo "Sorry, there was an error uploading your file.";
  102. verbose_error();
  103. }
  104. }
  105. }
  106. upload_image();
  107. ?>
  108. <a href="index.php">back</a>
  109. </body>
  110. </html>