What are common pitfalls when using PHP for image uploads and processing?

One common pitfall when using PHP for image uploads is not properly validating file types, which can lead to security vulnerabilities. To solve this, always check the file type before processing the upload. Another pitfall is not handling errors properly, which can result in broken image uploads. Make sure to implement error handling to provide feedback to users in case of issues.

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

// Handle errors during upload
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
    die('Upload failed with error code ' . $_FILES['image']['error']);
}