What are common pitfalls to avoid when using PHP for image upload scripts?

One common pitfall to avoid when using PHP for image upload scripts is not properly validating the file type. This can leave your application vulnerable to security risks such as file injections. To prevent this, always check the file type before allowing the upload to proceed.

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