linccce

Example code to be used

Oct 20th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1.     <?php
  2.     //phpinfo();
  3.      
  4.     error_reporting(E_ALL);
  5.     ini_set('display_errors',1);
  6.      
  7.     function isFastCGI () {
  8.         return !is_null($_SERVER['FCGI_SERVER_VERSION']);
  9.     }
  10.      
  11.     var_dump(isFastCGI());
  12.      
  13.     var_dump(getmyuid(), getmygid());
  14.      
  15.     var_dump(is_writable('.'));
  16.     var_dump(__DIR__);
  17.      
  18.      
  19.     ?>
  20.     <!DOCTYPE html>
  21.     <html>
  22.     <body>
  23.      
  24.     <form action="" method="post" enctype="multipart/form-data">
  25.         Select image to upload:
  26.         <input type="file" name="fileToUpload" id="fileToUpload">
  27.         <input type="submit" value="Upload Image" name="submit">
  28.     </form>
  29.      
  30.     <?php
  31.      
  32.      
  33.     if(isset($_FILES["fileToUpload"])){
  34.      
  35.     $target_dir = "uploads/";
  36.      
  37.     if(!file_exists(__DIR__.'/'.$target_dir)){
  38.      
  39.      
  40.     if(!mkdir(__DIR__.'/'.'uploads', 0755, true)) {
  41.             var_dump(error_get_last());
  42.     }
  43.      
  44.     }
  45.     $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  46.     $uploadOk = 1;
  47.     $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  48.     // Check if image file is a actual image or fake image
  49.     if(isset($_POST["submit"])) {
  50.         $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  51.         if($check !== false) {
  52.             echo "File is an image - " . $check["mime"] . ".";
  53.             $uploadOk = 1;
  54.         } else {
  55.             echo "File is not an image.";
  56.             $uploadOk = 0;
  57.         }
  58.     }
  59.     // Check if file already exists
  60.     if (file_exists($target_file)) {
  61.         echo "Sorry, file already exists.";
  62.         $uploadOk = 0;
  63.     }
  64.     // Check file size
  65.     if ($_FILES["fileToUpload"]["size"] > 500000) {
  66.         echo "Sorry, your file is too large.";
  67.         $uploadOk = 0;
  68.     }
  69.     // Allow certain file formats
  70.     if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
  71.         echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  72.         $uploadOk = 0;
  73.     }
  74.     // Check if $uploadOk is set to 0 by an error
  75.     if ($uploadOk == 0) {
  76.         echo "Sorry, your file was not uploaded.";
  77.     // if everything is ok, try to upload file
  78.     } else {
  79.         if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  80.             echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  81.         } else {
  82.             echo "Sorry, there was an error uploading your file.";
  83.             var_dump(error_get_last());
  84.         }
  85.     }
  86.      
  87.     }
  88.     ?>
  89.     </body>
  90.     </html>
Advertisement
Add Comment
Please, Sign In to add comment