天天看點

php下載下傳檔案(彈出下載下傳框)

<?php
	class DownloadHelper
	{
		private $fileName;
		private $fileSize;
		public function __construct($fileName)
		{
			$this->fileName=$fileName;	
			if(!file_exists($this->fileName))
			{
				die("檔案不存在");
				
			}
			$this->fileSize=filesize($this->fileName);
		}
		public function fileDownload()
		{		
			$fp=fopen($this->fileName,'r');
			
			//下載下傳檔案需要的頭
			header("Content-type:application/octet-stream");
			header("Accept-Ranges:bytes");
			header("Accept-Length:$this->fileSize");
			header("Content-Disposition:attachment;filename=".$this->fileName);
			
			$fileCount=0;
			$fileUnit=1024;
			while(!feof($fp)&&$this->fileSize-$fileCount>0)
			{
				$fileContent=fread($fp,$fileUnit);
				echo $fileContent;
				$fileCount+=$fileUnit;
				
			}
			
			fclose($fp);
				
		}
		
		
		
		
		
		
	}
	
	$downloadHelper=new DownloadHelper("pic1.jpg");
	$downloadHelper->fileDownload();

?>

           

下載下傳的思路是:1、檔案名字 2、檔案大小 3、下載下傳需要的http協定頭 4、fread函數