if (!file_exists($filename)){ //判断能否获取这个文件 header("Content-type: text/html; charset=utf-8"); echo "File not found!"; exit; } else { Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length: ".filesize($filename)); Header("Content-Disposition: attachment; filename=".basename($filename)); echo file_get_contents($filename); exit(); }
主要就是设置header做到的,
读取文件
方法1:file_get_contents()
方法2:fwrite($file,$filesize);
需要注意就是必须养成exit的好习惯