本帖最后由 kejzhao 于 2010-07-19 16:14:34 编辑

解决方案 »

  1.   

    你使用 readfile 要注意
    看看文件前后是否有空格
      

  2.   

    除了使用readfile来下载文件外,还有其他方式来下载文件吗?
      

  3.   


    下载文件前后没有空格
    要下载一个tar包,应该不存在前后空格的问题有环境的可以试一下,上面的程序可以直接调用的。
      

  4.   


    <?php
    /* 
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */download(dirname(__FILE__).'/Desktop.rar');function download($sFilePath){
        //解析文件路径,分离文件名
        $aFilePath = explode("/",str_replace("\\","/",$sFilePath));
        $sFileName = $aFilePath[count($aFilePath) - 1];
        //获取文件大小
        @$nFileSize = filesize($sFilePath);
        //设置文件头
        header("Pramga:public");
        header("Expires:0");
        header("Cache-Control:private");
        header("Cache-Component:must-revalidate,post-check=0,pre-check=0");
        header("Content-Disposition:attachment;filename=".$sFileName);
        header("Content-Length:".$nFileSize);
        @header("Content-type:".mime_content_type($sFilePath));
        header("Content-Encoding:none");
        header("Content-Transfer-Encoding:binary");
        //下载
        if(!connection_aborted() && readfile($sFilePath) != false)
           return 0;
        else
           return 1;
    }
    我下来测试了一下,可以实现啊,没有空格,rar也能正常打开。
    ?>
      

  5.   

    搞不懂要那么多header函数干什么,这里有个例子 http://www.vshu.com/archives/257例子中用了三个header函数,其实,第三个header的作用是保证IE在https协议下也能正常下载,如果在http协议下,两个header就够了。还有,例子中的文件是动态生成的,如果是实际存在的文件,把文件读进变量再输出就OK了。
      

  6.   

    补充一点,Content-type 指的是文件的MIME类型,比如文本文件要用 text/plain ,二进制文件要用 application/octet-stream,不能搞错