很多情况下需要隐藏真实下载地址进行给用户进行下载文件,直接通过文件路径下载的话会暴露完全地址,且做不到鉴权下载,所以这段代码就很好解决这个问题
/**
* 隐藏真实文件地址输出文件到浏览器下载
*
* @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;
}
}
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
一颗大萝北
本文地址: PHP隐藏真实文件下载地址
本文地址: PHP隐藏真实文件下载地址