<?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;为什么我这样写了之后运行他没有直接强制下载这个文件,反而读取了这个文件??

解决方案 »

  1.   

    你最好不要用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");
    这样应该可以解决你的问题
      

  2.   

    按照上面的方法改后出现的现象和之前一样没有强制下载,而是直接读取文件了
    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
    下面就是一大段乱码。。
      

  3.   

    我知道是怎么回事了,原来我的index.php中也包含了头,所以出了问题。