php强制下载文件

一些诸如 mp3 类型的文件,通常会在客户端浏览器中直接被播放或使用。如果你希望它们强制被下载,也没问题。可以使用以下代码:

 

function downloadFile($file){ 
    $file_name = $file; 
    $mime = 'application/force-download'; 
    header('Pragma: public');     // required 
    header('Expires: 0');        // no cache 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Cache-Control: private',false); 
    header('Content-Type: '.$mime); 
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Connection: close'); 
    readfile($file_name);        // push it out 
    exit(); 
}

Comments are closed.