I would like to allow users to be able to upload images to my website although i am having trouble implementing this feature.
<?php
if (isset($_POST['uploadImg'])) {
$image = $_FILES['image'];
$imageName = $_FILES['image']['name'];
$imageTmpName = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
$imageError = $_FILES['image']['error'];
$imageType = $_FILES['image']['type'];
$getImageExt = explode('.', $imageName);
$imageExt = strtolower(end($getImageExt));
$allowed = array('jpg', 'jpeg', 'png', 'tiff');
if (in_array($imageExt, $allowed)) {
if ($imageError === 0) {
if ($imageSize < 8000) {
$imageDestination = 'images/'.$imageName;
move_uploaded_file($imageTmpName, $imageDestination);
echo"uploaded successfully";
} else {
echo"File is to big";
}
} else {
echo "Error uploading file";
}
} else {
echo"Invalid file format";
}
}
?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype"multipart/form-data">
<input type="file" name="image">
<button type="submit" name="uploadImg">Upload</button>
</form>
This is the code I currently have, which is failing at the if statement which checks the image type. I have tried uploading all allowed image types but none are working
I tried implementing the script found at w3schools but this wasn't working for me. I also tried to use the fixes found here How to get the file extension in PHP? but i still couldn't get a solution.
Any help would be appreciated.
Also, is it possible to do this asynchronously?