php实现中文文件下载

PHP中文网
リリース: 2016-05-24 12:52:55
オリジナル
1157 人が閲覧しました

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

	}
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!