How can the readfile function be used to offer files for download without revealing direct links?
When offering files for download, it is important to not reveal direct links to the files as it can lead to security risks such as unauthorized access or hotlinking. One way to solve this issue is by using PHP to create a script that reads the file from the server and serves it to the user without exposing the direct link.
<?php
$file = 'path/to/file.txt';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>