dongmi0760 2016-10-11 18:13
浏览 55

为PDF文件生成唯一的下载链接

Hello everybody I'm new here please be friendly and don't get angry cause I'm a beginner.

To introduce you:

First I generated a pdf and save it after the user bought the file in a protected folder on my server. Than I save the path to this file with an id in my database.

Second have a php file where I show the user contents/entrys from a database like a download link for the file.

My link looks like this:

<a href="http://my-website.de/download?link=<?php echo $row["url_to_my_file"] ?>" target="_blank">Download you file</a>

When the user clicks on it he'll be redirected to my download.php where I get the url and send the user the download:

$my_file = file_get_contents($_GET['link']);
$size = strlen($my_file);
$file_name = 'My_file_name.pdf';

header('Content-Disposition: attachment; filename="' . $name . '"');
header('Content-Type: application/pdf');
header('Content-Length: ' . $size);
echo $my_file;

It works all fine but I have a big problem. When the user right clicks on the link he can see the path where the file is and he can download the file just like his mind (and not mine) and also when I lock the site where he can download the file because he know the download link.

No I get the idea to generate a md5 string and save it in my database when the user clicks on Download. That's my approach:

//Generate unique download key
$key = md5(uniqid(rand(), true));
//Insert into my database
$sql = "INSERT INTO download_keys (md5_key, expired, download_link) 
    VALUES ('$key', false, '$url')";

Seacond give the user a new link:

<a href="http://my-website.de/download?link='$key'" target="_blank">Download you file</a>

But I cant do this in my file where I show the user the files and don't know how to deal with more than one file?

This is my new download.php

$key = $_GET['key'];

$sql = "SELECT download_link FROM download_keys WHERE '$key' = md5_key AND expired = 'false'";
//Check if sql is empty or not
if (mysql_num_rows($sql)==0) {
    echo "No file found or key expired!";
} else {
    $link = $row["download_link"];
}
$sql = "UPDATE download_link SET expired = 'true' WHERE '$key' = md5_key";

At the end I change the expire value to true so when the user want to download the file with this link again it don't works.

Whats the right way to implement this unique file download function so that the user just see the key and not the path?

So all in all: I want to make a secret download link which the user can use only one. To download the file again he has do click the download button on the site even if he know the url

Thank you for your help! I hope you understan my problem. Thanks.

  • 写回答

1条回答 默认 最新

  • duannuochi3549 2016-10-11 18:20
    关注

    Ahh, do you mean how to display the link as "http://my-website.de/download?link=baksjcbasduo123231sdsad" instead of "http://my-website.de/download?link=baksjcbasduo123231sdsad.php" ?

    If that's the case: First, You'll need to create an .htaccess file

    Then put this in your .htaccess file:

    RewriteEngine On
    
    RewriteRule ^http://my-website.de/download/(.*)$ http://my-website.de/download?link=$1 [QSA,L]
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效