这个文件下载实例做得非常的详细他是结合header函数与while fread函数把文件分断读出来然后再发送到客户端了,算得上一个标准的文件下载实例。
一个PHP文件下载的小实例
/*======================================================
$FileName 为文件名称,必传
$FilePath 为文件路径.选填,可以为相对路径或者绝对路径
路径只能由英文跟数据组成,不能带有中文
如有问题 欢迎联系博主指出
======================================================*/
1) {
if (substr($FilePath, 0, 1) == '/') { //判断是否为绝对路径
$FilePath = $_SERVER['DOCUMENT_ROOT'] . $FilePath;
}
if (substr($FilePath, -1) != "/") { //检查最后是否为 / 结尾
$FilePath = $FilePath . '/';
}
if (is_numeric(strpos($FilePath, ":"))) { //检查是否为绝对路径
$FilePath = str_replace("/", "", $FilePath);
}
} elseif (strlen($FilePath) == 1 && $FilePath != "/") {
$FilePath = $FilePath . "/";
} else {
$FilePath = "";
}
if (!file_exists($FilePath . $FileName)) {
echo "下载失败:所要下载的文件未找到";
return;
}
/*================================================
发送下载相关的头部信息
=================================================*/
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes"); //按照字节大小返回
header("Accept-Length: $FileSize"); //返回文件大小
header("Content-Disposition: attachment; filename=" . $FileName); //这里客户端的弹出对话框,对应的文件名
/*================================================
开始下载相关
=================================================*/
$FileSize = filesize($FilePath . $FileName);
$File = fopen($FilePath . $FileName, "r"); //打开文件
$FileBuff = 512;
while ($FileSize >= 0) {
$FileSize-= $FileBuff;
echo fread($File, $FileBuff);
}
fclose($File);
?>总结
本下载实例并且支持中文文名了,在文件开头就进行了uft8编码转换了。
小邮包-包月订购包年服务网,该程序由好买卖商城开发,程序采用PHP+MYSQL架设,程序商业模式为目前最为火爆的包月订制包年服务模式,这种包年订购在国外网站已经热火很多年了,并且已经发展到一定规模,像英国的男士用品网站BlackSocks,一年的袜子购买量更是达到了1000万双。功能:1、实现多产品上线,2、不用注册也可以直接下单购买,3、集成目前主流支付接口,4、下单发货均有邮件提醒。
立即学习“PHP免费学习笔记(深入)”;










