生成唯一下载链接,仅下载一次

Ada*_*ith 9 php http

我想为我的用户创建一些独特的下载链接.原因是我只想让他们下载一次,这样他们就可以使用相同的链接再次下载.

我在数据库和标志字段中生成了一些密钥(例如,qwertyasdfghzxcbn.在下载链接中将像www.xxxxx.com/download.php?qwertyasdfghzxcbn一样),当用户下载时,它将更新1到旗帜领域.

我在网上搜索了一下,发现了这个. http://www.webvamp.co.uk/blog/coding/creating-one-time-download-links/

但这仅在您首先转到页面时才有效,然后只有页面才会生成唯一链接.我已经预先生成了我的数据库中的链接,我不需要再次重新生成,如果我在用户访问页面时生成密钥,他们将能够通过刷新页面多次下载.

aaa*_*789 9

解决方案是使链接目标本身成为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");
Run Code Online (Sandbox Code Playgroud)

您指向的任何链接只会指向download.php?fileid = 712984(无论fileid实际上是什么).这将是实际的下载链接,因为该脚本确实传输了文件; 但仅限于允许用户检索它.你必须自己写file_location(),check_permissions_for()然后mark_downloaded()自己动手.