Last active
December 24, 2023 09:38
-
-
Save no1k/8492575 to your computer and use it in GitHub Desktop.
Single image upload PHP
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Single Upload Form with PHP</title> | |
</head> | |
<body> | |
<form method="POST" action="upload.php" enctype="multipart/form-data"> | |
<label for="file"> Pick a file : </label> | |
<input type="file" name ="file"> | |
<input type="submit" value = "Upload"> | |
</form> | |
</body> | |
</html> |
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 | |
//Check if the file is well uploaded | |
if($_FILES['file']['error'] > 0) { echo 'Error during uploading, try again'; } | |
//We won't use $_FILES['file']['type'] to check the file extension for security purpose | |
//Set up valid image extensions | |
$extsAllowed = array( 'jpg', 'jpeg', 'png', 'gif' ); | |
//Extract extention from uploaded file | |
//substr return ".jpg" | |
//Strrchr return "jpg" | |
$extUpload = strtolower( substr( strrchr($_FILES['file']['name'], '.') ,1) ) ; | |
//Check if the uploaded file extension is allowed | |
if (in_array($extUpload, $extsAllowed) ) { | |
//Upload the file on the server | |
$name = "img/{$_FILES['file']['name']}"; | |
$result = move_uploaded_file($_FILES['file']['tmp_name'], $name); | |
if($result){echo "<img src='$name'/>";} | |
} else { echo 'File is not valid. Please try again'; } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
its help ful