What potential pitfalls should be considered when using PHP to generate files that are interpreted by browsers?

One potential pitfall to consider when using PHP to generate files interpreted by browsers is the risk of injection attacks if user input is not properly sanitized. To mitigate this risk, always sanitize user input and validate file extensions to prevent malicious code execution.

// Sanitize user input and validate file extension
$user_input = $_POST['user_input'];
$file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if (preg_match("/^[a-zA-Z0-9]+$/", $user_input) && in_array($file_extension, ['jpg', 'png', 'pdf'])) {
    // Generate file using sanitized input
    // Your file generation code here
} else {
    // Handle invalid input or file extension
    echo "Invalid input or file extension.";
}