How can PHP's header function be utilized to facilitate file downloads with user-friendly names?
To facilitate file downloads with user-friendly names using PHP's header function, you can set the Content-Disposition header with the filename parameter. This allows you to specify the name of the file that will be suggested to the user when they download the file.
<?php
$file = 'example.pdf';
$filename = 'my_custom_filename.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>
Related Questions
- Are there best practices for storing and retrieving data from databases in PHP classes?
- How does the PHP version and server API affect the functionality of $_SERVER['HTTP_REFERER'] when trying to retrieve the referring URL?
- What are some common mistakes to avoid when using mod_rewrite in PHP for URL manipulation?