如题。
我想用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]结果还是报错。请问为什么?