Home > php教程 > PHP源码 > body text

php实现中文文件下载

PHP中文网
Release: 2016-05-24 12:52:55
Original
1157 people have browsed it

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);

	}
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!