What are some common pitfalls when creating a simple image upload script in PHP?

One common pitfall when creating a simple image upload script in PHP is not validating the uploaded file to ensure it is actually an image. This can lead to security vulnerabilities such as allowing users to upload malicious files. To solve this issue, you should check the file type and ensure it is an image before allowing the upload to proceed.

// Check if the uploaded 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.');
}