What are common issues with PHP scripts that involve uploading and updating images?

Common issues with PHP scripts involving uploading and updating images include inadequate file type validation, lack of file size restrictions, and improper handling of file uploads. To solve these issues, ensure that the uploaded file is an image file, limit the file size to prevent server overload, and use secure methods for handling file uploads to prevent security vulnerabilities.

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

// Limit the file size to 5MB
$maxFileSize = 5 * 1024 * 1024; // 5MB in bytes
if ($_FILES['image']['size'] > $maxFileSize) {
    die('File size exceeds the limit of 5MB.');
}

// Handle file upload securely
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadFile)) {
    echo 'File uploaded successfully.';
} else {
    echo 'Failed to upload file.';
}