php 让浏览器下载文件

180it 2020-02-20 AM 1563℃ 0条
/**
 * 让浏览器下载文件
 *
 * @param string $file 文件路径
 * @param string $customName 自定义文件名
 * @return string | bool
 */
function browserDownload($file, $customName = null)
{
    if (file_exists($file)) {
        $filename = empty($customName) ? basename($file) : $customName;
        header('Content-length: ' . filesize($file));
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        @readfile($file);
    } else {
        return false;
    }
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 让浏览器下载文件