What are some common pitfalls to avoid when allowing users to upload files to a server using PHP?

One common pitfall to avoid when allowing users to upload files to a server using PHP is not properly validating the file type and size. This can lead to security vulnerabilities such as allowing users to upload malicious files or overwhelming the server with large files. To solve this issue, always validate the file type and size before allowing the upload to proceed.

// Validate file type and size before allowing upload
$allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
$maxFileSize = 5 * 1024 * 1024; // 5MB

if (in_array(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION), $allowedFileTypes) && $_FILES['file']['size'] <= $maxFileSize) {
    // Proceed with file upload
} else {
    // Display an error message to the user
    echo 'Invalid file type or size. Please upload a file with a valid format and size.';
}