我想通过点击来实现下载:<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,在本地运行就可以实现功能,一个文件跳出来提示保存
请大家帮忙看看,谢谢

解决方案 »

  1.   

    header 前面不要输出东西··

    <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> 
    这些去掉·
      

  2.   

    补充下,我的数据库格式一列fileurl 是固定地址:http://www.host.com/diretory/
    一列filesrc是文件名:1.txt,
    filedowUrl变量是将两者连接起来
      

  3.   

    <title> </title> 
    去掉的话那我网页标题没了,这个我想保留的
      

  4.   

    ob_start()先缓存起来也可以··
      

  5.   

    去掉了是可以,但是网页直接打开txt文件的内容,没跳出提示文件下载的对话框
      

  6.   

    信息的时候经常提示:cannot modify header information - headers already sent by (......)。其实已经实现需要的效果了,就是这个错误信息看着不爽,网上找了很多办法,综合使用得到的解决方法是
    1在页面顶部的php标签中加入ob_start();2在返回的信息下面加入ob_end_flush();这样就可以屏蔽错误信息的现实了http://hi.baidu.com/apxsoft/blog/item/c0640dd3e66697093bf3cf77.html
      

  7.   

    把html代码放到php代码后面试试。
      

  8.   

    如何让txt文件弹出下载而不是直接打开呢?我想让它弹出下载
      

  9.   

    把html和body的标签全部去掉试下··
      

  10.   

    全部去掉txt和doc还是直接打开
      

  11.   

    把全部html删掉, 如果确实要, 就把它写进字符串里, 在缓冲之后用echo 输出
      

  12.   

    问题解决了,
    header("Content-Type: application/force-download"); //设置类型
    header("Content-Disposition: attachment; filename=".basename($filename)); //设置位置信息
    加个这个可以了,谢谢大家