//有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件
//函数中使用了application/octet-stream头类型。
function downloads($filename,$dir='./')
{
$filepath = $dir.$filename;
if (!file_exists($filepath)) {
header("Content-type: text/html; charset=utf-8");
echo "File not found!";
exit;
} else {
$file = fopen($filepath, "r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: " . filesize($filepath));
Header("Content-Disposition: attachment; filename=".$filename);
echo "正在下载".fread($file, filesize($filepath));
}
}
downloads('a/a.sql');