donoworuq450547191 2017-06-23 23:43
浏览 64
已采纳

如何隐藏网站中的文件

I'm trying to hide my pdf files from users but I want them to be downloadable.
Heres my file structure

+-- index.php
+-- download.php
+-- content
    +-- .htaccess
    +-- files
       +-- pdf.pdf
       +-- pdf2.pdf

I tried to block users access to the content folder with .htaccess.

deny from all

But when i download pdf file with this

//download.php
header('Content-Type: application/pdf');
$file = "http://localhost/content/files/pdf2.pdf";
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");

Browser can't load it.
I can't figure out another way to do this

Users can upload files to the site and decide a price for it.
And when you have paid for it you can download it. There will be mysql query before downloading to check if user have bought it

  • 写回答

1条回答 默认 最新

  • doule6314 2017-06-24 00:20
    关注

    Your download.php file is just setting some headers to tell the browser to download the file. It doesn't actually write the content of the file in the response. You just have to add the following line of code to the end of download.php:

    readfile($_SERVER['DOCUMENT_ROOT'] . "/content/files/pdf2.pdf");
    

    NOTE: As gview mentioned in the comments, the proper way to do this would be to move the files outside the document root so they cannot be accessed regardless of your per-directory htaccess file. You can still use my solution, but instead of using $_SERVER['DOCUMENT_ROOT'], you would put the server path. Like this:

    readfile("/server/path/to/content/files/pdf2.pdf");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码