悉野小楼

导航

php服务生成exe附件下载

<?php
$realname = "zhuast.exe"; //原始文件名
$filename = "中文 文件名.exe"; //下载到浏览器的文件名

$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);

$ua = $_SERVER["HTTP_USER_AGENT"];
if(preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua)){
	header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
	header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
} else {
	header('Content-Disposition: attachment; filename="' . $filename . '"');
}

Header("Content-type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Content-Length:'.filesize($realname)); //指定下载文件的大小
 
//将文件内容读取出来并直接输出,以便下载
readfile($realname);
?>

php附件多3个字节问题, 保存的php文件, 要选择utf-8无bom格式, 如果php文件有bom, 生成的exe也会有bom

posted on 2016-12-21 17:33  悉野  阅读(860)  评论(0编辑  收藏  举报