Last active
June 27, 2022 11:21
-
-
Save etemesi254/600e08285a1b8fa999e66b1aab5d6d9c to your computer and use it in GitHub Desktop.
Upload a new file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// THIS FiLE WAS COPIED FROM w3schools | |
function upload(): int | |
{ | |
$target_dir = "./uploads/"; | |
$target_file = $target_dir . basename($_FILES["upload"]["name"]); | |
$uploadOk = 1; | |
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); | |
echo $target_file . "</br>"; | |
// Check if image file is a actual image or fake image | |
if (isset($_POST["submit"])) { | |
$check = getimagesize($_FILES["upload"]["tmp_name"]); | |
if ($check !== false) { | |
echo "File is an image - " . $check["mime"] . "."; | |
} else { | |
echo "File is not an image."; | |
$uploadOk = 0; | |
} | |
} | |
// Check if file already exists | |
// if (file_exists($target_file)) { | |
// echo "Sorry, file already exists."; | |
// $uploadOk = 0; | |
// } | |
// Check file size | |
if ($_FILES["upload"]["size"] > 50000000) { | |
echo "Sorry, your file is too large."; | |
$uploadOk = 0; | |
} | |
// Allow certain file formats | |
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" | |
&& $imageFileType != "gif") { | |
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; | |
$uploadOk = 0; | |
} | |
if ($uploadOk == 0) { | |
echo "Sorry, your file was not uploaded."; | |
} else { | |
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) { | |
echo "The file " . htmlspecialchars(basename($_FILES["upload"]["name"])) . " has been uploaded."; | |
} else { | |
$uploadOk=0; | |
print_r( $_FILES["upload"]);; | |
echo "Sorry, there was an error uploading your file."; | |
} | |
} | |
return $uploadOk; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment