Home > php教程 > php手册 > body text

利用 PHP 代码强制下载文件的类型

WBOY
Release: 2016-06-13 11:36:22
Original
984 people have browsed it

利用 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();
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template