加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP:提供下载文件而不提供直接链接

发布时间:2020-12-13 14:00:03 所属栏目:PHP教程 来源:网络整理
导读:我想提供下载发票.目前我使用的是简单的编号方案(invoice-01.pdf,invoice-02.pdf等).我知道我可以使用哈希来掩盖数据. 是否也可以使用PHP并通过不直接让用户指向它们来提供发票? 在 php.net甚至有一个例子 ?php// We'll be outputting a PDFheader('Content
我想提供下载发票.目前我使用的是简单的编号方案(invoice-01.pdf,invoice-02.pdf等).我知道我可以使用哈希来掩盖数据.

是否也可以使用PHP并通过不直接让用户指向它们来提供发票?

在 php.net甚至有一个例子
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

或者扩展一下

<?php
if ( can_this_file_be_downloaded() ) {
  header('Content-type: application/pdf');
  header('Content-Disposition: attachment; filename="invoice.pdf"');
  readfile("{$_GET['filename']}.pdf");
} else {
  die("None shall pass");
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读