php下载文件的问题
我想通过点击来实现下载:<a href="down.php?id=<? echo $id?>" target="_blank">下载</a>
down.php全代码如下(网上查来的):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
</head>
<body>
<?php
$id=$_GET['id'];
$SQL="select * from filedown where id=".$id;
$rs=mssql_query($SQL,$sqlLink);
list($id,$title,$puttime,$fileurl,$filesrc)=mssql_fetch_row($rs);
$filedownUrl = $fileurl.$filesrc;
if(strpos($filedownUrl, 'http://') !== false || strpos($filedownUrl, 'ftp://') !== false) {
header("Location: $filedownUrl");
exit();
}else {
$filename = basename($filedownUrl); //取文件名
$pathinfo = pathinfo($filename);
$filetype = $pathinfo['extension']; //取得扩展名
$filesize = filesize($filedownUrl);
if(ob_get_length() !== false) @ob_end_clean(); //清除以前的缓冲
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-type: '.$filetype);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-length: '.$filesize);
//readfile($filedownUrl);
$fp = @fopen($filedownUrl,"rb");
while(!feof($fp)) {
$buffer= fread($fp,8192);
echo $buffer;
}
@flush();
@ob_flush();
}
?>
</body>
</html>
但是运行后总出现Warning: Cannot modify header information - headers already sent by (output started at 错误
网上查了很多方法都不行,包括output_buffering = On 等等
如果把fileurl改成本地的,比如C:\Downloads\1.txt,在本地运行就可以实现功能,一个文件跳出来提示保存
请大家帮忙看看,谢谢