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 ();
分享到: 更多
------解決方案--------------------
http://baike.baidu.com/subview/126558/5073178.htm