- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
似乎用谷歌搜索 xsendfile 问题会产生许多相互矛盾/过时的结果。
为了全面披露,我正在考虑 xsendfile 1.0 beta,记录在 https://tn123.org/mod_xsendfile/beta/ (这个网站通常不会出现在搜索结果中,只有没有/beta 的网站才会出现)。我在 Linux 和 Windows 上将它与 apache 2.4 和 php 5.4.34 一起使用。除了是最新版本之外,我还需要使用测试版,因为只有测试版站点具有使用 VC9 为 apache 2.4 构建的 Windows 二进制文件。
我在阅读文档时犯了一个错误,标题中文件名值的描述是这样说的:
The value (file name) given by the header is assmumed to be url-encoded, i.e. unescaping/url-decoding will be performed. See XSendFileUnescape. If you happen to store files using already url-encoded file names, you must "double" encode the names... %20 -> %2520
XSendFIleUnescape 的描述是这样的:
Setting XSendFileUnescape off will restore the pre-1.0 behavior of using the raw header value, instead of trying to unescape/url-decode first.
关于相对路径的文档非常清楚地表明 X-SendFile
header 上的文件名应该是完整路径名。所以我通过 php 的 urlencode
函数仔细地运行我的路径名。
对我来说,最终结果总是在 Linux 和 Windows 上出现服务器内部错误(500 状态代码)。当我在 server config
上下文中有我的 XSendFilePath 指令时,文档说这是允许的,但我的错误日志中没有更具体的内容。但是当我(最终)将该指令移动到 Directory
上下文时,我逐渐在我的错误日志中得到了这个:
(404)Unknown error: [client 127.0.0.1:20742] xsendfile: bad file name encoding
最终,出于绝望,我说“去他妈的文档”,并删除了路径名上的 urlencode
。突然间它开始完美运行(Windows 和 Linux)!!!
我没有任何包含非 ASCII 字符的路径名,所以我已经准备好了。但我确实想知道应该应用哪种编码来允许非 ASCII 字符工作。如果你用谷歌搜索 xsendfile: bad file name encoding
,你会在 https://github.com/nmaier/mod_xsendfile/blob/master/mod_xsendfile.c 找到以下源代码该消息字符串是通过采用以下的真实分支生成的:
rv = ap_unescape_url(file);
if (rv != OK) {
但是我找不到ap_unescape_url()
的好的描述或源代码。除非 github 上的源代码已过时,否则该函数反对 PHP 的 urlencode()
函数执行的简单 % 编码。作为一个疯狂的猜测,我尝试调用 ap_escape_url()
,但它没有在 PHP 中定义。因此,问题是应该将什么编码应用于X-SendFile
header 中的路径名参数??
另一个观察/问题XSendFile
使用“apache internals”发送文件的描述可能会让您认为它会使用 mod_mime< 从文件扩展名构造一个
。但实际上并非如此,示例显示了对 Content-Type
header Content-Type
的显式 header()
调用。所以我的后续是从传递给 X-SendFile
的路径名构造该 header 的“正确”方法是什么,这样它就可以保证匹配 mod_mime
如果我们不使用 X-SendFile
会怎样?我能想出的最好的办法是使用 PHP 的 fileinfo
扩展名的以下代码——但据我所知,没有特别的理由期望它在给定文件名的 url 时实际上会匹配 apache 所做的事情.
$finfo = new finfo(FILEINFO_MIME);
$mime_info = $finfo->file($pathname);
if (! strlen($mime_info)) {
$mime_info = 'application/octet-stream; charset=binary';
}
$basename = basename($pathname);
$encoded = "$pathname";
header("Content-Type: $mime_info");
header("Content-Disposition: attachment; filename=\"$basename\"");
header("X-SendFile: $encoded");
最佳答案
我不会接受这个作为问题的答案,因为我还没有找到预期使用的“那个”编码函数。但我确实找到了这个古老的 Apache API 文档:http://pedrowa.weba.sk/docs/ApiDoc/apidoc_ap_unescape_url.html .它将 ap_unescape_url() 的返回值记录为:
Returns 0 on success, BAD_REQUEST if a bad escape sequence is found, or NOT_FOUND if %2F (/) is found.
当然,在 PHP 中,urlencode() 和 rawurlencode() 都将 '/' 编码为 %2F。
很明显,XSendFile header 中使用的任何完整路径名都必须不使用这些函数中的任何一个进行 urlencoded!!
我对我使用的“最佳”解决方案的猜测:
$encoded = str_replace('%2F', '/', rawurlencode($pathname));
我必须承认,我很惊讶 XSendFile 文档没有提到这一点。更令我惊讶的是,这个问题在这里没有得到任何答案。我应该在不同的 Stack Exchange 网站上发布这个吗?
关于php - xsendfile 期望什么编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769144/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!