很多情况下需要隐藏真实下载地址进行给用户进行下载文件,直接通过文件路径下载的话会暴露完全地址,且做不到鉴权下载,所以这段代码就很好解决这个问题


/**
 * 隐藏真实文件地址输出文件到浏览器下载
 *
 * @param [type] $strFilePath       文件路径,绝对路径或http/s url
 * @return void
 * @author 一颗大萝北 [email protected]
 */
function outputFile($strFilePath)
{
    if (file_exists($strFilePath)) {
        header(‘Content-Description: File Transfer’);
        header(‘Content-Type: application/octet-stream’);
        header(‘Content-Disposition: attachment; filename=’ . basename($strFilePath));
        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($strFilePath));
        ob_clean();
        flush();
        readfile($strFilePath);
        return true;
    } else {
        return false;
    }
}
说点什么
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...