PHP头条
热点:

php文件下载封装成一个类老是多出3个字节,请教错哪了


php文件下载封装成一个类 老是多出3个字节,请问哪里错了?

     class FileDown {
        public $fileName;
        public $fileSize;
         //转码 gb2312      
   function __construct($fileName){
   $this->fileName=iconv("utf-8","gb2312",$fileName);
   }
        function Down (){
         //$_SERVER['DOCUMENT_ROOT']当前运行脚本所在的文档根目录。在服务器配置文件中定义。
        $path=$_SERVER['DOCUMENT_ROOT']."/12/".$this->fileName;
        if(!file_exists($path)){
        die("文件不存在");
        }       
          $fp=fopen($path,"r");   //读入
            $this->fileSize=filesize($path); 
       //返回文件的头 浏览器靠头识别下载  //返回
       //返回的文件类型 流 可以是文本 二进制
   header("Content-type: application/octet-stream");
   //按照字节大小返回
   header("Accept-Ranges: bytes");
   //返回文件大小
   header("Accept-Length: $this->fileSize");
   //这里客户端的弹出对话框,对应的文件名
   header("Content-Disposition: attachment; filename=".$this->fileName);   
     $count=0;
    $buffer=1024;
    while(!feof($fp)&& $this->fileSize-$count>0){
             $fileData=fread($fp,$buffer);
             $count+=$buffer;
             echo $fileData;
     }
       fclose($fp);
    }
         }   
    $fd=new FileDown("白羊座.png");
     $fd->Down ();
分享到: 更多

相关文章

相关频道:

PHP之友评论

今天推荐