解决方案是使链接目标本身成为 PHP 脚本。
您会将实际文件隐藏在浏览器无法访问的某个地方(即,您可以通过 访问该文件fopen()
但不在文档根目录中的某个地方),并放置一个 download.php 文件来下载文件。
下载脚本本身看起来像这样:
$fileid = $_REQUEST['file'];
$file = file_location($fileid); // you'd write this function somehow
if ($file === null) die("The file doesn't exist");
$allowed = check_permissions_for($file, $fileid) // again, write this
// the previous line would allow you to implement arbitrary checks on the file
if ($allowed) {
mark_downloaded($fileid, $file); // so you mark it as downloaded if it's single-use
header("Content-Type: application/octet-stream"); // downloadable file
echo file_get_contents($file);
return 0; // running a return 0; from outside any function ends the script
} else
die("You're not allowed to download this file");
您指向的任何链接都只是指向 download.php?fileid=712984 (无论 fileid 实际是什么)。那将是实际的下载链接,因为该脚本确实传输了文件;但前提是允许用户检索它。不过,您必须自己编写file_location()
,check_permissions_for()
和mark_downloaded()
函数。