Advertisement
AviEzerzer

Untitled

Mar 21st, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  3.     //path to di
  4.     $dirPath = 'files/images/';
  5.     //file var
  6.     $file = $_FILES["file"]["name"];
  7.     //make dir if not exists
  8.     if (!file_exists($dirPath)) {
  9.         mkdir($dirPath, 0777, true);
  10.     }
  11.     //check if file uploaded before
  12.     if (file_exists($dirPath . $file)) {
  13.         //reject file
  14.         echo $file . " already exists. ";
  15.     } else {
  16.         $allowedExts = array("gif", "jpeg", "jpg", "png");
  17.         $parsedFile = explode(".", $file);
  18.         $extension = end($parsedFile);
  19.         //
  20.         print_r($parsedFile);
  21.         echo '<hr>' . $_FILES["file"]["type"] . '<hr>';
  22.         //
  23.         if (
  24.             ($_FILES["file"]["type"] == "image/gif")
  25.             || ($_FILES["file"]["type"] == "image/jpeg")
  26.             || ($_FILES["file"]["type"] == "image/jpg")
  27.             || ($_FILES["file"]["type"] == "image/png")
  28.         ) {
  29.             echo '<hr>' . $file . ' is of correct type: ' . $_FILES["file"]["type"] . ' <hr>';
  30.             //check if image is less than 1mb
  31.             if ($_FILES["file"]["size"] < 1000000) {
  32.                 echo '<hr>' . $_FILES["file"]["size"] . ' is below 20mb<hr>';
  33.                 if (in_array($extension, $allowedExts)) {
  34.                     echo 'in array';
  35.                     //store
  36.                     move_uploaded_file($_FILES["file"]["tmp_name"], $dirPath . $file);
  37.                     echo "Stored in: " . $dirPath . $file;
  38.                     echo '<img src='.$dirPath . $file.'>';
  39.                 } else {
  40.                     echo 'not in array!! REJECTED';
  41.                 }
  42.             } else {
  43.                 echo '<hr>' . $_FILES["file"]["size"] . ' is ABOVE 1mb!! REJECTED<hr>';
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement