如何在PHP中创建动态下载链接 [英] How to create Dynamic Download Link in PHP

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

问题描述

有没有一种方法可以在PHP中为单个文件创建一段时间的动态下载链接,或者该下载链接在该时间之后失效。在此之后,下载链接会更改。

Is there a way we can create a dynamic download link in PHP for a single file for some period of time or the download link expires after that time. After that period the download link changes.

实际上,我有一个要求,即只能通过特定电子邮件访问下载链接。由于文件的大小,我无法将其添加为附件。

Actually I have a requirement where the download link should be accessible only through a particular email. I can't add that file as an attachment because of its size.

有人可以帮我吗?

推荐答案

一个解决方案:


  1. 创建一个数据库表,该表存储一个很大的唯一值ID(随机),以及要下载的文件的名称/位置/内容。

  1. Create a Database table which stores a large unique ID (random), and the name/location/content of the file to download. Also include an expire date.

id                    | filename           | expires
----------------------+--------------------+--------------------
fsdhfs7dfsniuf92un3f2 | secret.doc         | 2012-03-23 23:32:32
sdf8shdf829nf32ufn23f | secret2.doc        | 2012-03-13 23:32:33


  • 通过电子邮件将链接发送给最终用户。 ..链接应该类似于:

  • Email a link to your end user... The link should be something like:

    http://yoursie.com/download/fsdhfs7dfsniuf92un3f2
    


  • 使用apache重写规则(mod_rewrite),它将捕获美观的链接并将其传递到PHP页面:

  • Use an apache rewrite rule (mod_rewrite) which will capture the nice looking link and pass it to a PHP page:

    RewriteEngine on
    RewriteRule ^/download/([a-z0-9]{20})$  /download.php?id=$1
    


  • 在该脚本中, download.php ,请查看 $ _ GET ['id'] 。运行数据库查询以查找记录。检查到期日期。如果一切正常,请继续。

  • In that script, download.php, look at $_GET['id']. Run a database query to look up the record. Check the expiration date. If all is OK, then proceed.

    要么使用PHP脚本输出正确的标头并下载文件,要么将内部重定向发送到前端终端代理(例如nginx),它将把下载过程卸载到nginx上,而不会将PHP与下载捆绑在一起。

    Either use the PHP script to output the correct headers and download the file, or send an internal redirect to a front-end proxy like nginx, which will offload the download process to nginx and not tie up PHP with the download.

    无论哪种方式,您都有一个安全的,可过期的链接,可以将其发送给最终用户。

    Either way, you have a secure, expireable link that you can send to your end users.

    保重!

    这篇关于如何在PHP中创建动态下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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