upload.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. function upload_image() {
  34. if (!$_FILES['file']) {
  35. return;
  36. }
  37. $target_file = IMAGE_DIR . '/' . basename($_FILES['file']["name"]);
  38. $uploadOk = 1;
  39. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  40. // Check if image file is a actual image or fake image
  41. if(isset($_POST["submit"])) {
  42. $check = getimagesize($_FILES['file']["tmp_name"]);
  43. if($check !== false) {
  44. echo "File is an image - " . $check["mime"] . ".";
  45. $uploadOk = 1;
  46. } else {
  47. echo "File is not an image.";
  48. $uploadOk = 0;
  49. }
  50. dbg("tmp name: " . $_FILES['file']["tmp_name"]);
  51. } else {
  52. dbg("submit is empty");
  53. }
  54. // Check if file already exists
  55. if (file_exists($target_file)) {
  56. echo "Sorry, file already exists.";
  57. $uploadOk = 0;
  58. }
  59. // Check file size
  60. if ($_FILES['file']["size"] > MAX_FILE_SIZE) {
  61. echo "Sorry, your file is too large. (" . $_FILES['file']["size"] . "/" . MAX_FILE_SIZE . ")";
  62. $uploadOk = 0;
  63. }
  64. dbg("file size: " . $_FILES['file']["size"]);
  65. // Allow certain file formats
  66. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  67. && $imageFileType != "gif" ) {
  68. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  69. $uploadOk = 0;
  70. }
  71. dbg("file type: " . $imageFileType);
  72. // Check if $uploadOk is set to 0 by an error
  73. if ($uploadOk == 0) {
  74. echo 'Sorry, your file was not uploaded.<br>';
  75. verbose_error();
  76. // if everything is ok, try to upload file
  77. } else {
  78. if (move_uploaded_file($_FILES['file']["tmp_name"], $target_file)) {
  79. $fileName = basename($_FILES['file']["name"]);
  80. $fileName = htmlspecialchars($fileName);
  81. echo 'The file <a href="/' . IMAGE_DIR . '/' . $fileName . '">' . htmlspecialchars($fileName) . '</a> has been uploaded.';
  82. } else {
  83. echo "Sorry, there was an error uploading your file.";
  84. verbose_error();
  85. }
  86. }
  87. }
  88. upload_image();
  89. ?>
  90. <a href="index.php">back</a>
  91. </body>
  92. </html>