天天看点

PHP X-sendfile实现文件下载

项目中涉及到文件的下载功能,通常PHP文件下载都是将文件先读取出来然后发送相应头文件进行下载。

如:

$file_dir = $instance->_attach_path.Helper_Hash::hashDIR($id).$attach['path'];
if(!file_exists($file_dir)){
    throw new FromMeException('文件不存在');
}
else{
    // 打开文件
    $file = fopen($file_dir,"r"); 
    // 输入文件标签
    Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: ".filesize($file_dir));
    Header("Content-Disposition: attachment; filename=" . $attach['name']);
    // 输出文件内容
    echo fread($file,filesize($file_dir));
    fclose($file);
    exit();
}
           

这种方式可以实现文件的下载,但是这种下载方式相当耗资源,长期占用服务端脚本资源和服务器内存资源,消耗很大。

后来查阅资料,我们可以借助X-sendfile模块来实现更高效率的文件下载。

X-sendfile是现代操作系统支持的一种高性能网络IO方式,服务端脚本程序比如php负责构造请求头信息,然后下载的时候不需要php参与,web服务器直接处理X-Sendfile头信息,并且把响应的文件直接发送给浏览器客户端;这样避免了内存占用。

首先我们要查看Apache的版本信息:

PHP X-sendfile实现文件下载

然后下载对应的X-sendfile模块包(下载地址:https://github.com/nmaier/mod_xsendfile)。

1、将解压出来的mod_xsendfile.so文件拷贝到Apache的modules目录下

2、配置Apache的conf目录下的httpd.conf文件,加入以下代码:

LoadModule xsendfile_module modules/mod_xsendfile.so

XSendFile on
XSendFilePath F:/
           

3、重启Apache服务,访问phpinfo()查看是否加载xsendfile模块:

PHP X-sendfile实现文件下载

这些工作完成之后我们就可以借助X-sendfile来实现文件下载了:

$file_dir = $instance->_attach_path.Helper_Hash::hashDIR($id).$attach['path'];
if(!file_exists($file_dir)){
    throw new FromMeException('文件不存在');
}
else{

    $ua = $_SERVER["HTTP_USER_AGENT"];

    $filename = $attach['name'];
    $encoded_filename = urlencode($filename);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);

    // 输入文件标签
    $realpath =  realpath($file_dir);
    header('Content-Type: application/octet-stream');


    //处理中文文件名,避免文件名乱码
    $encoded_filename = rawurlencode($filename);
    if (preg_match("/MSIE/", $ua)) {
        header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
        header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
    } else {
        header('Content-Disposition: attachment; filename="' . $filename . '"');
    }

    //让Xsendfile发送文件
    header("X-Sendfile: $realpath");
    exit();
}
           
需要注意的是:X-sendfile发送的文件路径要是绝对路径,不然会找不到。