How can PHP be used to dynamically generate file names for downloads based on user input?

When a user inputs data that needs to be downloaded, PHP can dynamically generate file names based on that input to provide a more personalized experience. This can be achieved by using PHP to concatenate the user input with a predetermined file name structure or by using functions like `uniqid()` to create unique file names for each download.

<?php
// Get user input (e.g. file name or ID)
$userInput = "example";

// Generate a unique file name based on user input
$fileName = $userInput . "_" . uniqid() . ".txt";

// Set appropriate headers for file download
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . $fileName);

// Output file content (e.g. read from a database or create dynamically)
echo "This is the content of the file.";

// Exit script to prevent any additional output
exit;
?>