Created
September 11, 2022 07:50
-
-
Save janus57/f0a82c870089c21fc581e5cfdf569a8f to your computer and use it in GitHub Desktop.
Simple PHP upload test
This file contains hidden or 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 | |
#### | |
#### Source : https://www.w3schools.com/php/php_file_upload.asp | |
#### Modified by : janus57 | |
#### | |
$target_dir = "uploads/"; | |
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); | |
$uploadOk = 1; | |
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); | |
// check if "uploads" folder exist, if not we create the folder | |
if (!file_exists('uploads/')) { | |
mkdir('uploads/', 0755, true); | |
} | |
// Check if image file is a actual image or fake image | |
if(isset($_POST["submit"])) { | |
$check = mime_content_type($_FILES['fileToUpload']['tmp_name']); | |
echo "File is : - " . $check . "."; | |
$uploadOk = 1; | |
} | |
// Check if file already exists | |
if (file_exists($target_file)) { | |
echo "Sorry, file already exists."; | |
$uploadOk = 0; | |
} | |
// Check file size - 100MB | |
if ($_FILES["fileToUpload"]["size"] > 100000000) { | |
echo "Sorry, your file is too large."; | |
$uploadOk = 0; | |
} | |
// Allow certain file formats | |
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "pdf") { | |
echo "Sorry, only JPG, JPEG, PNG, GIF & PDF files are allowed."; | |
$uploadOk = 0; | |
} | |
// Check if $uploadOk is set to 0 by an error | |
if ($uploadOk == 0) { | |
echo "Sorry, your file was not uploaded."; | |
// if everything is ok, try to upload file | |
} else { | |
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { | |
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; | |
} else { | |
echo "Sorry, there was an error uploading your file."; | |
} | |
} | |
?> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<body> | |
<form action="upload.php" method="post" enctype="multipart/form-data"> | |
Select file to upload: | |
<input type="file" name="fileToUpload" id="fileToUpload"> | |
<input type="submit" value="Upload File" name="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment