完整代码如下:
PHP文件下载_脚本学堂_www.jbxue.com$file_name="test_ftp.zip";
if(!file_exists($file_name)){
echo "文件不存在!";
return;
}
$file_size=filesize($file_name);
$brtype = $_SERVER["HTTP_USER_AGENT"];//获取客户端浏览器信息
//中文文件名需要编码处理
//$encoded_filename = urlencode($filename);
//$encoded_filename = str_replace("+", "%20", $encoded_filename);
//中文文件名,需要编码处理
header("Content-type: application/zip");//指定下载的文件类型为zip格式
header("Accept-Ranges: bytes");
header("Content-Length:".$file_size);
if (preg_match("/MSIE/", $brtype)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');//IE下如果处理中文文件名需要编码
} else if (preg_match("/Firefox/", $brtype)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '"');
} else {
header('Content-Disposition: attachment; filename="' . $file_name . '"');
}
$data=readfile($file_name);
echo $data;
?>
以上的代码主要是应用php header函数实现文件的下载,在php中有很多的文件内容类型可以这样操作,大家有空多研究下了。