|
一共分为三个部分
第一: html 下载 页面 filedownlist.php
<a href='fileProcess.php?filename=love.jpg'>点击下载</a><img src="love.jpg" width="70px">
<br/><br/>
<a href='fileProcess.php?filename=love.jpg'>点击下载</a><img src="love.jpg" width="70px">
<br/><br/>
<a href='fileProcess.php?filename=love.jpg'>点击下载</a><img src="love.jpg" width="70px">
<br/><br/>
第二: 一个 实现 PHP 下载 功能的函数 fileDownload.php
<?php
// 函数 文档
// 参数 说明 $file_name 文件名
// $file_sub_name 下载文件的 子路径 “”
function down_file($file_name,$file_sub_dir)
{
//如果文件 是中文的
// 创建文件
//原因: php文件函数 比较古老需要转码gb2312
//
//$file_name="赵.jpg";
//如果文件 是中文的
// 创建文件
//原因: php文件函数 比较古老需要转码gb2312
$file_name=iconv("gb2312","gb2312",$file_name);
$file_path=$file_sub_dir.$file_name;
// 打开文件
if(!file_exists($file_path))
{
echo "文件不存!";
return ;
}else
{
// open file
//r 只读 方式
$fp=fopen($file_path,"r" );
// 获取文件的大小
$file_size=filesize($file_path);
//返回的 文件形式
header("Content-type: application/octet-stream");
//返回 字节的 大小
header("Accept-Ranges:bytes");
//返回文件的 大小
header("Accept=Length:$file-size");
//这里 客户端的 弹出对话框 对应的文件名
header("Content-Disposition:attachment;filename=".file_name);
//向客户端 会送数据
$buffer="1024";
//为了下载的安全 我们最好做一个 文件字节读取计数器
$file_count=0;
// 判断 文件 是否结束?
while(!feof($fp)&&($file_size-$file_count>0))
{
$file_data = fread($fp,$buffer);
//统计 读了 多少个字节?
$file_count+=$buffer;
//部分数据 会送 给 浏览器哦!
echo $file_data;
}
//关闭文件
fcolse($fp);
}
}
// 测试 函数 是否可用?
down_file("love.jpg","./image/");
?>
第三: 调用 fileDownload.php 关联 filedownlist.php 文件!
<?php
// 第一 种方法! 不封装到 类
require "fileDownload.php";
// 接受 要下载的 文件名字
$file_name=$_REQUEST['filename'];
// 调用
down_file($file_name,'./image/');
// 封装到类
?><?php
// 第一 种方法! 不封装到 类
require "fileDownload.php";
// 接受 要下载的 文件名字
$file_name=$_REQUEST['filename'];
// 调用
down_file($file_name,'./image/');
// 封装到类
?>
|
|
|