标题:
php 文件下载源码
[打印本页]
作者:
haoren
时间:
2015-9-14 20:39
标题:
php 文件下载源码
下载文件的方法:
实列一:
<?php
header
(
"Content-Type: text/html;charset=utf-8"
);
$file_name
=
"jdao.png"
;
//使用相对路径
// $file_path = "./pic/".$file_name;
// 使用绝对路径,下载速度要更快些
$file_path
=
$_SERVER
[
'DOCUMENT_ROOT'
].
"/pic/"
.
$file_name
;
if
(
!
file_exists
(
$file_path
)){
echo
"文件不存在"
;
return
;
}
$fp
=
fopen
(
$file_path
,
"r"
);
$file_size
=
filesize
(
$file_path
);
//echo $file_size;
header
(
"Content-Type: application/octet-stream"
);
//返回为文件形式
header
(
"Accept-Ranges: bytes"
);
header
(
"Accept-Length:
$file_size
"
);
header
(
"Content-Disposition: attachment; filecolor:#c82829">$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
;
}
fclose
(
$fp
);
// echo $file_path; 这句话不能放在下载前,否则就执行不了下载
?>
实例2:
为了兼容中文文件名不出现乱码,并做成函数源码如下:
<?php
//首行前不能空出
//文件下载参数说明
//$file_name: 下载文件名
//$file_sub_path: 子文件夹路径
function
fileDown(
$file_name
,
$file_sub_path
){
//$file_name = iconv("utf-8","gb2312",$file_name); //IGNORE
$file_path
=
$_SERVER
[
'DOCUMENT_ROOT'
].
$file_sub_path
.
$file_name
;
if
(
!
file_exists
(
$file_path
)){
echo
"文件不存在"
;
return
;
}
$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; filewidth:14px;color:#8e908c">弹
出
下
载
对
话
框
//以下为了兼容中文文件名在不同浏览器中显示而做如下修改
$ua
=
$_SERVER
[
"HTTP_USER_AGENT"
];
$encoded_filename
=
urlencode
(
$file_name
);
$encoded_filename
=
str_replace
(
"+"
,
"%20"
,
$encoded_filename
);
header
(
'Content-Type: application/octet-stream'
);
if
(
preg_match
(
"/MSIE/"
,
$ua
)) {
header
(
'Content-Disposition: attachment; filecolor:#c82829">$encoded_filename
.
'"'
);
}
else
if
(
preg_match
(
"/Firefox/"
,
$ua
)) {
header
(
'Content-Disposition: attachment; filename*="utf8
\'\'
'
.
$file_name
.
'"'
);
}
else
{
header
(
'Content-Disposition: attachment; filecolor:#c82829">$file_name
.
'"'
);
}
$buffer
=
11024
;
$file_count
=
0
;
while
((
!
feof
(
$fp
))
&&
(
$file_size
-
$file_count
>
0
)){
$file_data
=
fread
(
$fp
,
$buffer
);
$file_count
+=
$buffer
;
//把数据回送给浏览器
echo
$file_data
;
}
fclose
(
$fp
);
}
fileDown(
"电脑图片.jpg"
,
"/pic/"
);
?>
测试文件下载
测试中文文件下载
将文件下载封装在类文件里filedown.class.php,源码如下
<?php
class
filedown{
protected
$file_name
;
public
$file_sub_path
;
function
__construct(
$file_name
,
$file_sub_path
){
$this
->
file_name
=
$file_name
;
$this
->
file_sub_path
=
$file_sub_path
;
}
function
fileDown(){
//$file_name = iconv("utf-8","gb2312",$file_name); //IGNORE
$file_path
=
$_SERVER
[
'DOCUMENT_ROOT'
].
$this
->
file_sub_path .
$this
->
file_name;
if
(
!
file_exists
(
$file_path
)){
echo
"文件不存在"
;
return
;
}
$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; filewidth:14px;color:#8e908c">弹
出
下
载
对
话
框
//以下为了兼容中文文件名在不同浏览器中显示而修改的
$ua
=
$_SERVER
[
"HTTP_USER_AGENT"
];
$encoded_filename
=
urlencode
(
$this
->
file_name);
$encoded_filename
=
str_replace
(
"+"
,
"%20"
,
$encoded_filename
);
header
(
'Content-Type: application/octet-stream'
);
if
(
preg_match
(
"/MSIE/"
,
$ua
)) {
header
(
'Content-Disposition: attachment; filecolor:#c82829">$encoded_filename
.
'"'
);
}
else
if
(
preg_match
(
"/Firefox/"
,
$ua
)) {
header
(
'Content-Disposition: attachment; filename*="utf8
\'\'
'
.
$this
->
file_name .
'"'
);
}
else
{
header
(
'Content-Disposition: attachment; filecolor:#c82829">$this
->
file_name .
'"'
);
}
$buffer
=
11024
;
$file_count
=
0
;
while
((
!
feof
(
$fp
))
&&
(
$file_size
-
$file_count
>
0
)){
$file_data
=
fread
(
$fp
,
$buffer
);
$file_count
+=
$buffer
;
//把数据回送给浏览器
echo
$file_data
;
}
fclose
(
$fp
);
}
}
// $file1 = new filedown("电脑图片.jpg","/pic/");
// $file1->filedown();
?>
演示多文件下载
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1