文件下载可以通过Header文件头来实现,本方法是介绍基于强制性方法进入文件下载,不管遇到的文件是否属于下载类型,都将转入下载,以下为实现代码。
<?php
function force_download($file)
{
if ((isset($file))&&(file_exists($file))) {
header("Content-length: ".filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$file");
} else {
echo "404!没有文件可下载";
}
}
?>
用时候,可以把需要下载的文件路径作为函数参数
echo force_download("/2015/test.rar");