What are some potential pitfalls to be aware of when handling file uploads in PHP?

One potential pitfall when handling file uploads in PHP is not properly validating the file type and size before processing it. This can lead to security vulnerabilities such as allowing users to upload malicious files or overwhelming the server with large uploads. To mitigate this risk, always validate the file type and size before moving the file to the desired location on the server.

// Validate file type and size before moving the file
$allowedTypes = ['image/jpeg', 'image/png'];
$maxFileSize = 2 * 1024 * 1024; // 2MB

if (in_array($_FILES['file']['type'], $allowedTypes) && $_FILES['file']['size'] <= $maxFileSize) {
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    echo 'File uploaded successfully.';
} else {
    echo 'Invalid file type or size.';
}