如何建立可下载文件的临时链接 [英] How to make temporary link for a downloadable file

查看:517
本文介绍了如何建立可下载文件的临时链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我制作一个临时下载链接的项目,以保护文件免受热链接的攻击。<​​/ p>

我相信这样做是可能的。因为我们都知道许多文件共享站点 不想提及任何 ...它们的文件链接已过期...



例如。



如果我从他们的网站下载了一个文件,他们会提供直接链接以单击该文件吗?
,但该链接将在几个小时或几分钟后失效。



我怎么知道该链接已失效?如果一天之后我在下载管理器上复制了相同的链接,它将无法下载相同的文件。



我已经在中做到了这一点。 htaccess



Ex。

  RewriteRule。* \。(rar | ZIP)$ http://domain.com [R,NC] 

如果他们将直接链接复制到浏览器的地址栏中,则会在 http://domain.com中重定向



但是,如果他们在其下载管理器上复制了直接链接,则文件将被下载。



如果他们将链接发布到论坛网站,博客等其他任何网站,
,并要求读者将链接复制并粘贴到他们的下载管理器中,以便他们可以下载,该怎么办?



这是我要防止保护文件的问题。我在PHP上执行此操作,但我无法弄清楚...



非常感谢您的帮助。

解决方案

使用类似这样的代码

  $ path = uploads / ; 
$ actualfilename = $ path。$ filename;
$ fakefilename = downloadfile.pdf;
if($ typeofview == download){
@readfile($ actualfilename);
标头(内容类型:应用程序/ pdf);
标头( Content-Disposition:附件; filename =。$ fakefilename。);
标头( Content-Transfer-Encoding:二进制);
标头( Content-Length:。filesize($ actualfilename));
标头(接受范围:字节);
出口;

将此添加到您的.htaccess文件中

  RewriteEngine On 
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule fakefile.php(。*)$ orignalfile.php?$ 1 [L,QSA]


I am making a project for a temporary download link for me to protect the file from hotlinkers...

I believe that this is possible to do so.. because we all know that many of file sharing site "don't wanna mention any"... their link for the file has expiration...

Ex.

If I download one file from their site they give a direct link to click it right? but then that link will expire right after a few hours or minutes.

How should I know that the link was expired? If I copy the same link on my download manager after one day, it can't download the same file.

I already made this possible in htaccess.

Ex.

RewriteRule .*\.(rar|ZIP)$ http://domain.com [R,NC]

if they copy the direct link in the address bar of the browser they will be redirected in http://domain.com

But then if they copy the direct link on their download manager the file will be downloaded.

What if they post the link into any other site like forum website, blog, etc., and ask the reader to copy and paste the link into their download manager, so that they can download it directly.

This is the problem I want to prevent to protect my file. I am doing this on PHP but I can't figure it out...

Your help is very much appreciated.

解决方案

Use some code like this

 $path="uploads/";
                  $actualfilename=$path.$filename;
                  $fakefilename="downloadfile.pdf";
                   if($typeofview=="download") {
                          @readfile($actualfilename);
                        header('Content-type: application/pdf');
                        header('Content-Disposition: attachment; filename="' . $fakefilename . '"');
                        header('Content-Transfer-Encoding: binary');
                        header('Content-Length: ' . filesize($actualfilename));
                        header('Accept-Ranges: bytes');
                        exit;

add this to your .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule fakefile.php(.*)$ orignalfile.php?$1 [L,QSA]

这篇关于如何建立可下载文件的临时链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆