//ob_start();
for($i=0;$i<10000;$i++)
{
$content .="见鬼";
}
$fn= "down.txt";
$size = strlen($content);
      header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
//Force the download
header("Content-Disposition: attachment; filename=txt.txt;" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size);
    echo $content;
    // flush(); 
   // ob_clean();如果开启ob_start();则原样输出内容
如果不开启Warning: Cannot modify header information - headers already sent by (由于我需要下载的内容是直接从数据库中查询,所有不想把内容写入到文件,在下载文件。
在此求教直接下载方法。即把要下载的内容保存在变量中,然后下载

解决方案 »

  1.   

    没太明白什么意思,警告是因为你ob_start()前面有输出
    后面你说的不太明白,下载不是给用户么?最终不还是依然给用户存到文件中么?
      

  2.   

    因为你已经在haeder之前有输出,所以出错,你只需要把之前输出的内容做一次清理即可.代码如下:ob_start();
    ob_clean();
    for($i=0;$i<10000;$i++)
        {
            $content .="见鬼";
        }
        $fn= "down.txt";
        $size = strlen($content);
          header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: public"); 
            header("Content-Description: File Transfer");
            header("Content-Type: application/force-download");
            //Force the download
            header("Content-Disposition: attachment; filename=txt.txt;" );
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ". $size);
               echo $content;
               // flush(); 
      

  3.   


    现在又内容 $CONTENT = "内容内容内容内容内容内容内容内容";不用写入到文件file.txt 中,就可以直接下载。
    也就是用户在下载时,不必要先写入到文件中,然后再下载
      

  4.   

    <?php 
    ob_start();
    for($i=0;$i<10000;$i++)
        {
            $content .="见鬼";
        }
        $fn= "down.txt";
        $size = strlen($content);
          header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: public"); 
            header("Content-Description: File Transfer");
            header("Content-Type: application/force-download");
            //Force the download
            header("Content-Disposition: attachment; filename=txt.txt;" );
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ". $size);ob_clean();
               echo $content;
      

  5.   

    理解错了,看这个
    <?php 
    ob_start();
    for($i=0;$i<10000;$i++)
        {
            $content .="见鬼";
        }
        $fn= "down.txt";
        $size = strlen($content);
          header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: public"); 
            header("Content-Description: File Transfer");
            header("Content-Type: application/octet-stream"); // 这里修改了下文件类型
            //Force the download
            header("Content-Disposition: attachment; filename=txt.txt;" );
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ". $size);ob_clean();
               echo $content;
      

  6.   

    你的什么浏览器?我这里 chrome firefox ie6.0/7.0都是会正常下载
      

  7.   

    楼主现在php的下载都是header下载,不会出现先保存文件在下载的,楼主你估计说的是超级链接那种吧
      

  8.   


    汗啊,IE7 ff 360 都不可以下载,谢谢了,那我看看