PHP下载浏览器能直接打开的文件

public function download(){
    //下载路径
    $template_url = './xx/xx/xx.txt';
    $filename = 'xx.txt';
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $filename);
    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($template_url));
    //清除缓存,否则在下载的文件里有缓存中的内容
    ob_end_clean();
    readfile($template_url);
}

猜你喜欢

转载自blog.csdn.net/yixu0534/article/details/80815315