Home > Backend Development > PHP Tutorial > PHP强制下载的有关问题

PHP强制下载的有关问题

WBOY
Release: 2016-06-13 13:27:24
Original
803 people have browsed it

PHP强制下载的问题
$file_dir = 'd:/file/';
$file_name = 'XXX.zip';
$file=fopen($file_dir.$file_name,"r"); 
header("Content-Type: application/force-download");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($file_dir.$file_name));
header("Content-Disposition: attachment; filename=".$file_name);
// 输出文件内容
echo fread($file,filesize($file_dir.$file_name));
fclose($file);
exit;

为什么我这样写了之后运行他没有直接强制下载这个文件,反而读取了这个文件??

------解决方案--------------------
你最好不要用fread($file,filesize($file_dir.$file_name));
你把
echo fread($file,filesize($file_dir.$file_name));
fclose($file);
换成
readfile($file_dir.$file_name);
试试
最好再添加以下两句
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
这样应该可以解决你的问题

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template