【问题标题】:download files with spaces in their name - restriction through htaccess下载名称中带有空格的文件 - 通过 htaccess 进行限制
【发布时间】:2015-08-05 14:50:58
【问题描述】:

我偶然发现了一种奇怪的文件保护方法。

在受保护的文件夹中基本上有一个空的 download.php 文件和两个导致文件被下载的文件(从同一文件夹): 存储密码的 .htpwd 和 .htaccess:

RewriteEngine On
##RewriteBase /
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteRule \.(.*)$ - [NC,F,L]

RewriteCond %{QUERY_STRING} ^filename=(.*)$
RewriteRule ^download\.php$ %1
RewriteRule (.*) - [E=file:$1]
Header set Content-type "octet-stream"
Header set Content-disposition "attachment; filename=%{file}e" env=file

##AuthName "Restricted Access" 
##AuthType Basic
##AuthUserFile /local/home/example/example.com/downloads/.htpwd
##AuthGroupFile /dev/null 
##require valid-user

下载链接示例:
http://www.example.com/downloads/download.php?filename=dog%20cat%20cow.zip

如果文件名是 dog-cat-cow.zip - 一切正常。当文件名是时问题开始: “dog cat cow.zip”然后文件将被下载,但下载后的文件名将是“dog”而不是“dog cat cow.zip”。所以问题是人们得到的文件甚至没有扩展名并且不知道如何处理它。

考虑到我根本无法重命名这些文件并删除空格,如何解决这个问题?我不是如何阅读 .htaccess 的专家,而且我能想到的搜索短语没有任何类似的问题解决方案。 有什么想法吗?

【问题讨论】:

  • 我建议你这样做Header set Content-disposition "attachment; filename='%{file}e'" env=file,引号中的文件名
  • 正如@anonymous 所说,由于文件名有空格,因此您需要引号分隔符。但是,额外的e 是为了什么?
  • “但下载后的文件名” - 您可以让浏览器始终提示下载位置(推荐) - 在那个阶段用户可以更正文件名。
  • 第一条评论中的技巧完成了这项工作,但只有当我在新浏览器中尝试时。有什么好方法可以强制清除这个 htaccess 的缓存?
  • 缓存是针对特定请求的,即。 /downloads/download.php?filename=dog%20cat%20cow.zip,而不是“for this .htaccess”——许多已经下载了该文件的用户会想要再次下载它吗?这样做的问题是,如果用户已经缓存了它,那么你就无能为力了。从现在开始,您只能最小化新请求的缓存时间。但是,您将永远不会从这些下载请求的客户端缓存中受益。

标签: php apache .htaccess .htpasswd


【解决方案1】:

如 cmets 中所述,也许可以尝试:

RewriteEngine On
##RewriteBase /
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteRule \.(.*)$ - [NC,F,L]

RewriteCond %{QUERY_STRING} ^filename=(.*)$
RewriteRule ^download\.php$ %1
RewriteRule (.*) - [E=file:$1]
Header set Content-type "octet-stream"
Header set Content-disposition "attachment; filename='%{file}'" env=file

##AuthName "Restricted Access" 
##AuthType Basic
##AuthUserFile /local/home/example/example.com/downloads/.htpwd
##AuthGroupFile /dev/null 
##require valid-user

另外,为什么不在download.php 文件本身中设置内容类型和处置?

$fileStorageDirectory = '/the/folder/the/downloadable/files/are/in/';

// Check for Parameter
if( !isset( $_GET['filename'] ) || $_GET['filename']=='' ){
  header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
  die('No File Specified');
}

$fileName = trim( $_GET['filename'] );
$fileRequested = $fileStorageDirectory.$fileName;
// Check File Exists
if( !file_exists( $fileRequested ) ){
  header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
  die('File Not Found');
}
// Check File is Readable
if( !is_readable( $fileRequested ) ){
  header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
  die('File Not Accessible');
}
// Send the File
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="'.fileName.'"' );
readfile( $fileRequested );
exit;

【讨论】:

    猜你喜欢
    • 2011-09-25
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多