What are some common pitfalls when handling file uploads in PHP?

One common pitfall when handling file uploads in PHP is not validating the file type. This can lead to security vulnerabilities if malicious files are uploaded. To prevent this, always check the file type before moving the uploaded file to the desired location.

// Validate file type before moving the uploaded file
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
$uploadedFileType = $_FILES['file']['type'];

if (!in_array($uploadedFileType, $allowedTypes)) {
    die('Invalid file type. Only JPEG, PNG, and GIF files are allowed.');
}

// Move the uploaded file to the desired location
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);