How can PHP beginners ensure that the downloaded file retains its original name when using headers for file downloads?
When using headers for file downloads in PHP, beginners can ensure that the downloaded file retains its original name by setting the "Content-Disposition" header with the value "attachment; filename=[original_filename]". This tells the browser to prompt the user to save the file with its original name instead of a generic one.
<?php
$file = 'path/to/your/file.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . basename($file));
readfile($file);
exit;
?>