是用来备份数据库的,生成一个txt文件,直接弹出对话框给用户下载。用了很多了,大致都是在HEADER里面定义然后输出缓冲,但经过客户使用,发现很不稳定,有的时候会提示下载,有的时候就是直接输入内容,请问大家都是怎么做的??我使用过网易邮箱的附件下载,发现非常稳定,从来都没有出现过直接打开附件的问题。

解决方案 »

  1.   

    $fname=$_GET["fname"];
    function do_download($downloadfile){
      $file = fopen($downloadfile,"r");
      if (!$file) 
         {
         echo "文件找不到";
     } 
      else
         {
         if(ereg("http://",$downloadfile) || ereg("ftp://",$downloadfile)) 
           {
            Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
            Header("Content-Disposition: attachment; filename=".basename($downloadfile));
            while (!feof($file))
      {
               //echo fread($file,1024);
       echo fgets($file,1024);
      }
       fclose ($file);
    }
      else
        {
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Content-Disposition: attachment; filename=".basename($downloadfile));
            echo fread($file,filesize($downloadfile));
            fclose($file);
            }
     }
    fclose ($file);
    }do_download($fname);
      

  2.   

    <a href="http://www.ccidcom.com/blog/u/632/index.html">我的博客</a>