Hi all I have a simple image uploading script for my website with a few checks to be taken before the image is uploaded. If I try to upload for instance a gif or a large file the correct error message will be displayed but the image will still continue to upload. Although this shouldn't be happening as for the error message to be displayed there would have to be a value in the array? Any advice?
<?php
$message = "";
$errors = array();
if (isset($_POST['uploadImg'])) {
// getting data needed from the image
$image = $_FILES['image'];
$imageName = $_FILES['image']['name'];
$imageTmpName = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
$imageError = $_FILES['image']['error'];
$imageType = $_FILES['image']['type'];
$imageInfo = getimagesize($_FILES['image']['tmp_name']);
$imageWidth = $imageInfo[0];
$imageHeight = $imageInfo[1];
// seperates the image name from the image extension
$getImageExt = explode('.', $imageName);
// puts all extensions to lowercase to prevent exceptions e.g. JPEG not being accepted
$imageExt = strtolower(end($getImageExt));
// array containg the allowed file types
$allowed = array(
'jpg',
'jpeg',
'png',
'tiff'
);
// checks if the file extension exists in the array
if (!in_array($imageExt, $allowed)) {
$errors[ext] = "Invalid file format";
}
// checks if there are any errors with the image
if (!$imageError === 0) {
$errors[image] = "Error with your file";
}
// checks if the file size is less than 1MB
if (!$imageSize < 1000000) {
$errors[size] = "File is to big";
}
// check if the image width is less than or equal to 1024px
if (!$imageWidth <= 1024) {
$errors[width] = "Image width is limited to 1024px for layout purposes";
}
// check if image height is less than or equal to 768 pixels
if (!$imageHeight <= 768) {
$errors[height] = "Image height is limited to 768px for layout purposes";
}
if (empty($errors)) {
// inserts the uploaded image into the 'images' directory
$imageDestination = 'images/' . $imageName;
move_uploaded_file($imageTmpName, $imageDestination);
} else {
$message = "Something Went Wrong";
}
}
?>