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

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

WBOY
WBOYOriginal
2016-06-13 12:29:55919browse

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn