Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. <?php
  2. // Check if the form was submitted
  3. if($_SERVER["REQUEST_METHOD"] == "POST"){
  4. // Check if file was uploaded without errors
  5. if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
  6. $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
  7. $filename = $_FILES["photo"]["name"];
  8. $filetype = $_FILES["photo"]["type"];
  9. $filesize = $_FILES["photo"]["size"];
  10.  
  11. // Verify file extension
  12. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  13. if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
  14.  
  15. // Verify file size - 5MB maximum
  16. $maxsize = 5 * 1024 * 1024;
  17. if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
  18.  
  19. // Verify MYME type of the file
  20. if(in_array($filetype, $allowed)){
  21. // Check whether file exists before uploading it
  22. if(file_exists("upload/" . $filename)){
  23. echo $filename . " is already exists.";
  24. } else{
  25. move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename);
  26. echo "Your file was uploaded successfully.";
  27. }
  28. } else{
  29. echo "Error: There was a problem uploading your file. Please try again.";
  30. }
  31. } else{
  32. echo "Error: " . $_FILES["photo"]["error"];
  33. }
  34. }
  35. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement