好的,所以我正在尝试在我的网站上创建一个下载链接生成器。我的意思是:
-
- 用户写入"列表X",站点列出给定目录中的每个文件
-
- 每个文件都有一个数字(从 0 到 n)
-
- 例如,当用户键入:"下载 5"时
-
- 它将用户重定向到下载脚本
-
- 该脚本使用户有一个弹出的下载"框"
我首先想到我可以创建一个包含每个文件名的数组,然后将该数组中的位置用于"下载 X"命令。
因此,当他将用户重定向到下载脚本时,具有用户要下载的文件名称的变量被"POST"到下载脚本,并且标题会因此而更改。
所以这是我的两个问题:
1) - 如何根据用户输入更改下载脚本?2) - 输入字段已经用于其他目的,带有"$_SERVER['PHP_SELF']",所以我不知道如何在没有表单的情况下"POST"变量?
这是我的简单下载脚本:
<?php
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('huge_document.pdf');
?>
提前感谢!
不要让他们输入任何东西。使每次下载都成为他们点击的链接。在链接的 URL 中,您将拥有一个包含下载唯一标识符的查询字符串。像这样:
<a href="/path/to/downloadscript.php?id=5">Down file #6</a>
然后downloadscript.php
将从$_GET
超全局获取 ID,您可以从那里开始(使用示例中提到的数组):
<?php
$download_id = (int) $_GET['id']; // 5
$files = array(
file1.pdf,
file2.pdf,
file3.pdf,
file4.pdf,
file5.pdf,
file6.pdf // This is the file they'll get
);
$filename = $files[$download_id];
// get the file name from your array or database
header('Content-disposition: attachment; ' . filename=$filename);
header('Content-type: application/pdf');
readfile($filename);
?>