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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效