|
分享一个php下载函数,用于下载指定的文件,不限文件格式。有需要的朋友参考下。
php下载文件的函数举例,如下:
科汛网上商城管理系统
查看详情
一个经过完善设计有着及其强大的会员互动和独特创新的内容管理系统。主要功能模块包括:文章频道、图片频道、下载频道、动漫频道、音乐频道、影视频道、商城频道、供求频道、采集管理 、专题频道等等。系统通用模块:用户管理、博客日志管理、相册管理、音乐盒管理、朋友圈管理、广告管理、公告管理、模板管理、网站信息配置、高级自定义SQL扩展标签,RSS在线订阅功能、网站统计、邮件列表、邮件群发、数据库管理、站内短消
0
<?php
/**
* php下载文件
* $filename=$_SERVER['DOCUMENT_ROOT']."/test/file/20130503/20130503150036_85945.doc";
* $title="下载测试9";
* down($filename,$title);
* edit by bbs.it-home.org
*/
//$filename 必须是路径,不是url
function down($filename,$title=''){
$file = fopen($filename,"r");
$filesize = filesize($filename);
$encoded_filename = urlencode($title);
$encoded_filename = str_replace("+", "%20", $title);
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/is", $ua)) {
$file_name = urlencode($title);
header("Pragma: public"); header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename='.$file_name);
} else {
header('Content-Type: application/octet-stream');
if (preg_match("/Firefox/is", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $title . '"');
} else {
header('Content-Disposition: attachment; filename="' . $title . '"');
}
}
echo fread($file, $filesize);
fclose($file);
}
?>登录后复制 运行效果如下图:
|
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号