久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標(biāo)題:
php 文件下載源碼
[打印本頁(yè)]
作者:
haoren
時(shí)間:
2015-9-14 20:39
標(biāo)題:
php 文件下載源碼
下載文件的方法:
實(shí)列一:
<?php
header
(
"Content-Type: text/html;charset=utf-8"
);
$file_name
=
"jdao.png"
;
//使用相對(duì)路徑
// $file_path = "./pic/".$file_name;
// 使用絕對(duì)路徑,下載速度要更快些
$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
);
//彈出下載對(duì)話(huà)框
//向客戶(hù)端發(fā)送數(shù)據(jù)
$buffer
=
1024
;
$file_count
=
0
;
while
((
!
feof
(
$fp
))
&&
(
$file_size
-
$file_count
>
0
)){
$file_data
=
fread
(
$fp
,
$buffer
);
$file_count
+=
$buffer
;
//把數(shù)據(jù)回送給瀏覽器
echo
$file_data
;
}
fclose
(
$fp
);
// echo $file_path; 這句話(huà)不能放在下載前,否則就執(zhí)行不了下載
?>
實(shí)例2:
為了兼容中文文件名不出現(xiàn)亂碼,并做成函數(shù)源碼如下:
<?php
//首行前不能空出
//文件下載參數(shù)說(shuō)明
//$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">彈
出
下
載
對(duì)
話(huà)
框
//以下為了兼容中文文件名在不同瀏覽器中顯示而做如下修改
$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
;
//把數(shù)據(jù)回送給瀏覽器
echo
$file_data
;
}
fclose
(
$fp
);
}
fileDown(
"電腦圖片.jpg"
,
"/pic/"
);
?>
測(cè)試文件下載
測(cè)試中文文件下載
將文件下載封裝在類(lèi)文件里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">彈
出
下
載
對(duì)
話(huà)
框
//以下為了兼容中文文件名在不同瀏覽器中顯示而修改的
$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
;
//把數(shù)據(jù)回送給瀏覽器
echo
$file_data
;
}
fclose
(
$fp
);
}
}
// $file1 = new filedown("電腦圖片.jpg","/pic/");
// $file1->filedown();
?>
演示多文件下載
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
欧美精品一区在线观看
|
日日操夜夜干
|
国产无人区一区二区三区
|
999视频在线播放
|
亚洲欧美视频在线观看
|
亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区
|
中文字幕高清
|
欧美一区二区网站
|
亚洲香蕉
|
亚洲综合99
|
一区二区国产在线观看
|
久久精品国产精品青草
|
欧美一级免费看
|
亚洲最新网址
|
天天操网
|
久久天堂
|
日韩91
|
欧美成人高清视频
|
天天影视综合
|
国产欧美精品一区
|
欧洲一级黄
|
在线免费国产视频
|
黄色一级大片在线免费看产
|
91在线网站
|
www.久久
|
xxxxx黄色片
|
免费h在线
|
99日韩
|
日韩精品999
|
国产精品国产a级
|
黄色福利
|
日本久久福利
|
国产精品欧美一区二区三区
|
国产精品久久久久一区二区三区
|
成人国产精品一级毛片视频毛片
|
久久新
|
亚洲人成人一区二区在线观看
|
在线视频久久
|
在线观看av不卡
|
一区二区在线免费播放
|
国产精品福利在线观看
|