guohao14 2010-10-27 14:46
浏览 270
已采纳

Php实现下载功能过程中出现错误Warning: Cannot modify header information

如题。
我想用php下载服务器某一个文件夹下的文件。
实现代码如下【downfile.php】:
[code="java"]
$path = "d:\wwww\program\";
$filename="test.rar";
$durl = $path.$filename;
//$filename = 'phpcms2008_o2abf32efj883c91a.iso';
$file = @fopen($durl, 'r');
header("Content-Type: application/octet-stream");

header("Accept-Ranges: bytes");

header("Accept-Length: ".filesize($durl));

header("Content-Disposition: attachment; filename=".$filename);

echo fread($file,filesize($durl));
fclose($file);
[/code]
提示错误
Warning: Cannot modify header information - headers already sent by (output started at D:\WebRoot\oem\style\default\header.php:1) in D:\WebRoot\oem\module\download\downfile.php on line 32

我就根据网上找到的方法试了一下。我的只能通过 修改php.ini 来达到目的。output_buffering=on。
请问还有其他的方法吗?
我还想用ob_start();
[code="java"]
<?php ob_start(); ?>
<?php
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor. */ $filetype = $_REQUEST['filetype']; //echo $filetype; $path = dirname(__FILE__)."/../../seq/"; //$durl = $path; $filename = ""; if($filetype == ""||$filetype == null){ echo "当前文件不存在!"; }else{ $filename = "test.rar"; $durl = $path.$filename; //$filename = 'phpcms2008_o2abf32efj883c91a.iso'; $file = @fopen($durl, 'r'); header("Content-Type: application/octet-stream");

header("Accept-Ranges: bytes");

header("Accept-Length: ".filesize($durl));

header("Content-Disposition: attachment; filename=".$filename);

echo fread($file,filesize($durl));
fclose($file);
ob_end_flush();
?>
[/code]结果还是报错。请问为什么?

  • 写回答

2条回答 默认 最新

  • enet_java 2010-10-27 15:07
    关注

    你的解决办法就是最正确的了,MS已经找不到更好的办法了 :idea:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配