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;
?>