upload.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. session_start();
  3. $upload=1;
  4. include 'header.php';
  5. echo "<br>";
  6. echo '<main id="content">';
  7. ?>
  8. <form action="upload.php" method="post" title="Upload Image" enctype="multipart/form-data">
  9. Select image to upload:
  10. <input type="file" name="fileToUpload" title="Select File" id="fileToUpload"> <br>License:
  11. <input type="text" name="license" title="License" value="CC By SA 4.0"> <br> Description (important for accessability!):
  12. <input type="text" name="alt" title="Alt Text of Image" onfocus="this.value=''" value="Please provide a short description of the image"> <br>
  13. <input type="submit" value="Upload Image" title="Submit" name="submit">
  14. </form>
  15. <?php
  16. if(isset($_POST['submit']))
  17. {
  18. if($_SESSION['Name']!="" && $_SESSION['Level']>=$ImagePermissionLevel)
  19. {
  20. $target_dir = "images/";
  21. $target_file = $target_dir . bin2hex(random_bytes(5)) . basename($_FILES["fileToUpload"]["name"]);
  22. $uploadOk = 1;
  23. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  24. // Check if image file is a actual image or fake image
  25. if(isset($_POST["submit"])) {
  26. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  27. if($check !== false) {
  28. echo "File is an image - " . $check["mime"] . ".";
  29. $uploadOk = 1;
  30. } else {
  31. echo "File is not an image.";
  32. $uploadOk = 0;
  33. }
  34. }
  35. // Check if file already exists
  36. if (file_exists($target_file)) {
  37. echo "Sorry, file already exists.";
  38. $uploadOk = 0;
  39. }
  40. // Check file size
  41. if ($_FILES["fileToUpload"]["size"] > 500000) {
  42. echo "Sorry, your file is too large.";
  43. $uploadOk = 0;
  44. }
  45. // Allow certain file formats
  46. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  47. && $imageFileType != "gif" ) {
  48. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  49. $uploadOk = 0;
  50. }
  51. // Check if $uploadOk is set to 0 by an error
  52. if ($uploadOk == 0) {
  53. echo "Sorry, your file was not uploaded.";
  54. // if everything is ok, try to upload file
  55. } else {
  56. $target_file = $target_dir . bin2hex(random_bytes(5)) . '.' . $imageFileType;
  57. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  58. $sql = "INSERT INTO Images (Link, Unlocked, Uploader, License, Alt) VALUES (?, '0', ?, ?, ?)";
  59. if($_SESSION['Level']>=$ImageAutoLevel)
  60. $sql = "INSERT INTO Images (Link, Unlocked, Uploader, License, Alt) VALUES (?, '1', ?, ?, ?)";
  61. $stmt = $conn->prepare($sql);
  62. $in1=$target_file;
  63. $in2=intval($_SESSION['ID']);
  64. $in3=$_POST['license'];
  65. $in4=$_POST['alt'];
  66. $stmt->bind_param("siss", $in1, $in2, $in3, $in4);
  67. $stmt->execute();
  68. echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded. It will appear in the gallery as soon as it has been checked by the moderation team.";
  69. } else {
  70. echo "Sorry, there was an error uploading your file.";
  71. }
  72. }
  73. }
  74. else
  75. {
  76. echo "No permission for image upload";
  77. }
  78. }
  79. ?>
  80. </main>
  81. </body>
  82. </html>