What are common pitfalls when using PHP for file uploads?

One common pitfall when using PHP for file uploads is not properly validating the file type or size, which can lead to security vulnerabilities. To solve this, always validate the file type and size before processing the upload.

if ($_FILES['file']['size'] > 1000000) {
    echo "File is too large. Please choose a smaller file.";
    exit;
}

$allowedTypes = array('image/jpeg', 'image/png', 'image/gif');
if (!in_array($_FILES['file']['type'], $allowedTypes)) {
    echo "Invalid file type. Please upload a JPEG, PNG, or GIF file.";
    exit;
}

// Proceed with file upload and processing