php 文件下载

文件下载方法简单实现:

function file_down($file, $downName = null, $extension = null){
	if(!file_exists($file)){
		echo "File [ $file ] Not Found...";
		exit;
	}

	// 下载名
	if(!isset($downName)){
		$info = pathinfo($file);
		$downName = $info['filename'];	//php 5.2之后		
	}

	// 后缀名
	if(!isset($extension)){
		$info = pathinfo($file);
		$extension = $info['extension'];
	}

	$down_file = fopen($file, 'r');
	Header("Content-type: application/octet-stream");
	Header("Accept-Ranges: bytes");
	Header("Accept-Length: " . filesize($file));	// 文件大小
	Header("Content-Disposition: attachment; filename = " . $downName . "." . $extension);	// 文件名及后缀名
	echo fread($down_file, filesize($file));
	fclose($down_file);
}

 

发表评论