您的位置:首页 > 编程语言 > PHP开发

PHP强制下载文件

2017-07-28 08:47 316 查看
header() 函数向客户端发送原始的 HTTP 报头。进入指定页面后就弹出文件下载对话框,如图



代码如下:

[php]
view plain
copy

function download_file($archivo, $downloadfilename = null) {  
   
    if (file_exists($archivo)) {  
        $downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo);  
        header('Content-Description: File Transfer');  
        header('Content-Type: application/octet-stream');  
        header('Content-Disposition: attachment; filename=' . $downloadfilename);  
        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($archivo));  
   
        ob_clean();  
        flush();  
        readfile($archivo);  
        exit;  
    }  
   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: