>  기사  >  php教程  >  php实现中文文件下载

php实现中文文件下载

PHP中文网
PHP中文网원래의
2016-05-24 12:52:551157검색

php代码

/*
 * $filepath 文件路径
 * $newfilename 下载时的命名
 * download('/abc.xls','abc教学');
 * 下载下来的文件将是abc教学.xls
 */
function download($filepath,$newfilename){		
		$id = intval($_GET['id']);
		$db = model('tool');
		$tool = $db->where('id='.$id)->find();
		if(!$tool) $this->error('未找到您要查看的工具');
		
		$ua = $_SERVER["HTTP_USER_AGENT"];
		
			
		$pathinfo = pathinfo($filepath);
		$newfilename = $newfilename.'.'.$pathinfo['extension'];
		
		$file = fopen( $filepath ,"r");

		header('Content-Type: application/octet-stream');
		header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($filepath));
		if (preg_match("/MSIE/", $ua)) {
			header('Content-Disposition: attachment; filename="' . rawurlencode($newfilename)  . '"');
		} else if (preg_match("/Firefox/", $ua)) {
			header('Content-Disposition: attachment; filename*="utf8\'\'' . $newfilename . '"');
		} else {
			header('Content-Disposition: attachment; filename="' . rawurlencode($newfilename) . '"');
		}
		
        echo fread($file, filesize($filepath));
		
		//$db->where('id='.$id)->data($update)->update();
        fclose($file);

	}
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.