<?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;为什么我这样写了之后运行他没有直接强制下载这个文件,反而读取了这个文件??
$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;为什么我这样写了之后运行他没有直接强制下载这个文件,反而读取了这个文件??
你把
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");
这样应该可以解决你的问题
Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\interface\index.php:8) in E:\xampp\htdocs\interface\download.php on line 4Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\interface\index.php:8) in E:\xampp\htdocs\interface\download.php on line 5Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\interface\index.php:8) in E:\xampp\htdocs\interface\download.php on line 6Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\interface\index.php:8) in E:\xampp\htdocs\interface\download.php on line 7Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\interface\index.php:8) in E:\xampp\htdocs\interface\download.php on line 8Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\interface\index.php:8) in E:\xampp\htdocs\interface\download.php on line 9
下面就是一大段乱码。。