Advertisement
ZacharyVincze

File Upload

Jul 28th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2. $target_dir = "uploads/";
  3. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  4. $new_target_file = $target_dir . "banner." . pathinfo($target_file, PATHINFO_EXTENSION);
  5. $uploadOk = 1;
  6. $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
  7.  
  8. //Check if file exists
  9. if (file_exists($target_file)) {
  10.     echo "Sorry, file already exists.<br>";
  11.     $uploadOk = 0;
  12. }
  13.  
  14. //Check file size
  15. if ($_FILES["fileToUpload"]["size"] > 5000000) {
  16.     echo "Sorry, your file is too large. (5mb)<br>";
  17.     $uploadOk = 0;
  18. }
  19.  
  20. //Allow certain file formats
  21. if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG") {
  22.     echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br>";
  23.     $uploadOk = 0;
  24. }
  25.  
  26. if (isset($_POST["submit"])) {
  27.     $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  28.     if ($check !== false) {
  29.         echo "File is an image - " . $check["mime"] . ".<br>";
  30.         $uploadOk = 1;
  31.     } else {
  32.         echo "File is not an image.<br>";
  33.         $uploadOk = 0;
  34.     }
  35. }
  36.  
  37. if ($uploadOk == 0) {
  38.     echo "Sorry, your file was not uploaded.";
  39. } else {
  40.     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_target_file)) {
  41.         echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.<br>";
  42.     } else {
  43.         echo "Sorry, there was an error uploading your file.<br>";
  44.     }
  45. }
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement