cdw1p

Hard Filter PHP - Prevent Bypass Extension

Jul 15th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2. $target_dir = "konten/";
  3. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  4. $uploadOk = 1;
  5. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  6. // Check if image file is a actual image or fake image
  7. if(isset($_POST["submit"])) {
  8.     $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  9.     if($check !== false) {
  10.         echo "File is an image - " . $check["mime"] . ".";
  11.         $uploadOk = 1;
  12.     } else {
  13.         echo "File is not an image.";
  14.         $uploadOk = 0;
  15.     }
  16. }
  17. // Check if file already exists
  18. if (file_exists($target_file)) {
  19.     echo "Sorry, file already exists.";
  20.     $uploadOk = 0;
  21. }
  22. // Check file size
  23. if ($_FILES["fileToUpload"]["size"] > 500000) {
  24.     echo "Sorry, your file is too large.";
  25.     $uploadOk = 0;
  26. }
  27. // Allow certain file formats
  28. if($imageFileType != "pdf" && $imageFileType != "PDF") {
  29.     echo "Sorry, only PDF files are allowed.";
  30.     $uploadOk = 0;
  31. }
  32. // Check if $uploadOk is set to 0 by an error
  33. if ($uploadOk == 0) {
  34.     echo "Sorry, your file was not uploaded.";
  35. // if everything is ok, try to upload file
  36. } else {
  37.     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  38.         echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  39.     } else {
  40.         echo "Sorry, there was an error uploading your file.";
  41.     }
  42. }
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment