How can PHP developers ensure secure image/bilder uploads on a website?

To ensure secure image/bilder uploads on a website, PHP developers can implement measures such as validating file types, checking file sizes, and storing uploaded files outside the web root directory to prevent direct access.

// Check if the file is an image
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['file']['type'], $allowedTypes)) {
    die('Invalid file type. Only JPEG, PNG, and GIF files are allowed.');
}

// Check file size
$maxFileSize = 5 * 1024 * 1024; // 5MB
if ($_FILES['file']['size'] > $maxFileSize) {
    die('File size is too large. Maximum file size allowed is 5MB.');
}

// Move uploaded file to a secure directory outside the web root
$uploadDir = '/path/to/uploaded/files/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
    echo 'File uploaded successfully.';
} else {
    echo 'Error uploading file.';
}