dourang6858 2014-03-20 20:00
浏览 57
已采纳

如何访问受htaccess保护的某个php页面的文件?

I have a page ( example : http://mypage.com/getfiles.php ) where the user can enter a keycode and then the page generates the links( html http://mypage.com/download/myfile.zip) to the files zu download and the user can click on the links to get the files.

The problem is that the files are stored in a folder ( download ) which is protected so nobody can directly access the files under http://mypage.com/download/myfile.zip.

I tried some differnt thinks i read on the internet to change the .htaccess file to alow access from a certain page... but it didn't work !

So I have 2 Questions : 1. Is this the easiest way ? Or is it better to use another way to give the user access to the files ( filesize from 50mb to 500mb ) 2. If the idea to customize the .htaccess file is the best way whats wrong with it ?

# set an environtment variable "noauth" if the request starts with certain string
SetEnvIf Request_URI "^http://mypage.com/getfiles.php" noauth=1

AuthType Basic
AuthName "Access to /download"
AuthUserFile ****
Require user ****

# Here is where we allow/deny
Order Deny,Allow
Satisfy any
Deny from all
Require valid-user
Allow from env=noauth

Or do i have to guide the user to an php script which forwards him to the file, because with link it doesn't work ?

Thank you very much !

  • 写回答

2条回答 默认 最新

  • dongshi9489 2014-03-20 20:14
    关注

    You can create an script say download.php under http://mypage.com/ which eventaully copy content from file "download/myfile.zip" and auto download it to the user.

    Example code:

    // Add some keycode security, then download file for user.
    
    $file_url = 'download/myfile.zip';
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
    readfile($file_url); // do the double-download-dance (dirty but worky)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私