How can the file name be changed during download in PHP?
When downloading a file in PHP, you can change the file name by setting the "Content-Disposition" header with the desired file name. This can be achieved by using the header() function before outputting the file contents. By specifying the "attachment" type and the file name in the header, the browser will download the file with the specified name.
<?php
$file = 'example.txt';
$newFileName = 'new_filename.txt';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $newFileName . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>