【问题标题】:X-SendFile on Apache2 (PHP) serving 0B file, no errors thoughApache2 (PHP) 上的 X-SendFile 服务 0B 文件,但没有错误
【发布时间】:2011-08-20 18:35:43
【问题描述】:

我安装了mod_xsendfile,好像成功了; xsendfile.load 出现在 /etc/apache2/mods-enabled 中,我在运行测试脚本时没有发现任何错误。但是,每次我运行它时,都会得到一个 0B 文件。

这是我的测试脚本:

$file = 'sample.mp4';
$path = '/var/storage/media/'.$file;
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-type: application/octet-stream");
header("X-Sendfile: $path");

很明显,我有一个文件存储在 /var/storage/media/sample.mp4 中,它只有 25MB,如果我这样做的话,效果会非常好:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;

我在 /var/storage 和 /var/www 的 .htaccess 文件中也有这个(包含所有这些的文件都存储在 /var/www/files/index.php 中):

XSendFile on
XSendFileAllowAbove on

就像我说的那样,我没有收到任何错误,PHP 可以肯定地访问该文件,但我必须在 x-sendfile 配置中遗漏一些东西……这提醒了我,我注意到几乎每个 mod 都启用了 mods有.load和.conf,但xsendfile只有.load,但其他几个也有,那和它有什么关系吗?

谢谢。

【问题讨论】:

  • 当你说你没有收到错误时,这是​​否意味着你已经尝试过 "tail -f /var/log/apache2/errors.log"
  • 我通读了它,没有发现任何与它试图加载的文件有关的东西。发现很多关于它的条目试图获取我尚未创建的 favicon 文件。
  • 嘿,是的,我也收到了那个图标错误

标签: php apache2 download x-sendfile


【解决方案1】:

我遇到了同样的问题,这个解决方案对我有用:

安装模块后,您需要启用它并提供进入 Apache 配置的访问路径(httpd.conf 用于全局配置,或特定站点 vhosts 用于特定配置),如下所示:

# enable xsendfile
XSendFile On

# enable sending files from parent dirs
XSendFilePath /var/www/

当然,您必须将“/var/www/”替换为您希望 xsendfile 可以访问的任何文件夹

【讨论】:

  • 起初我只尝试添加XSendFile On,但我还必须指定XSendFilePath,因为我的重写规则开始表现得很奇怪。谢谢!
【解决方案2】:

如果使用 Apache,请确保在 Apache 配置中也打开 XSendFile。如果您不这样做,它看起来会正常工作,但会提供空文件。

例如:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    XSendFile on
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

【讨论】:

    【解决方案3】:

    我在 Ubuntu 14.04 和 Apache/2.4.7 上使用它。 重要的是:

    1. 在每个主机配置上设置:
    <Directory /var/www/path/to/files>
        XSendFile On
        XSendFilePath /var/www/path/to/files
    </Directory>
    
    1. 添加到 .htaccess:
    XSendFile on
    

    虽然我只使用了 1;它让我下载空文件。 添加2后,效果很好。

    【讨论】:

      【解决方案4】:

      在 Windows 上,我必须使用以下内容才能在 doc roo 之外提供文件。 其他一切都给了我 404

      XSendFilePath C:/

      【讨论】:

        【解决方案5】:

        尝试将 header("X-Sendfile: $path"); 作为文件中的第一个 header() 调用。

        也看看这个。似乎是一个类似的问题,它可能会给你一些想法:

        X-Sendfile Error

        【讨论】:

        • 我阅读了建议并尝试了其中一些(删除类型标头,切换到 X-LIGHTTPD-send-file),但同样的问题。
        • 您尝试将 X-Sendfile 标头移动到标头列表的顶部?
        • 是的。以各种组合添加和删除其他标题,没有变化。好吧,除了当我摆脱 Content-Disposition 标头时文件名是“下载”之外。
        • 嗯,那我难住了,对不起。
        • X-Sendfile 是否真的没有运行但不会抛出任何错误?我不确定如何检查。