有时候我们要在网站上下载一些类似于TXT、png..等文件时,浏览器会默认打开而不是下载,当然,我们可以把文件打包后再上传下载,这也是一种方法。我在这里分享一个PHP强制下载的函数。
function downloadFile($file){
/*Coded by Alessio Delmonti*/
$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();
}
另外,在这里分享一种HTML中的强制下载。
HTML5 的 Download 属性用来强制浏览器下载对应文件,而不是打开.