<?php
class
traverseDir{
public
$currentdir
;
public
$filename
;
public
$fileinfo
;
public
$savepath
;
public
function
__construct(
$curpath
,
$savepath
){
$this
->currentdir=
$curpath
;
$this
->savepath=
$savepath
;
}
public
function
scandir(
$filepath
){
if
(
is_dir
(
$filepath
)){
$arr
=scandir(
$filepath
);
foreach
(
$arr
as
$k
=>
$v
){
$this
->fileinfo[
$v
][]=
$this
->getfilesize(
$v
);
}
}
else
{
echo
"<script>alert('当前目录不是有效目录');</script>"
;
}
}
public
function
getfilesize(
$fname
){
return
filesize
(
$fname
)/1024;
}
public
function
tozip(
$items
){
$zip
=
new
ZipArchive();
$zipname
=
date
(
'YmdHis'
,time());
if
(!
file_exists
(
$zipname
)){
$zip
->open(
$savepath
.
$zipname
.
'.zip'
,ZipArchive::OVERWRITE);
for
(
$i
=0;
$i
<
count
(
$items
);
$i
++){
$zip
->addFile(
$this
->currentdir.
'/'
.
$items
[
$i
],
$items
[
$i
]);
}
$zip
->close();
$dw
=
new
download(
$zipname
.
'.zip'
,
$savepath
);
$dw
->getfiles();
unlink(
$savepath
.
$zipname
.
'.zip'
);
}
}
}
class
download{
protected
$_filename
;
protected
$_filepath
;
protected
$_filesize
;
protected
$savepath
;
public
function
__construct(
$filename
,
$savepath
){
$this
->_filename=
$filename
;
$this
->_filepath=
$savepath
.
$filename
;
}
public
function
getfilename(){
return
$this
->_filename;
}
public
function
getfilepath(){
return
$this
->_filepath;
}
public
function
getfilesize(){
return
$this
->_filesize=number_format(
filesize
(
$this
->_filepath)/(1024*1024),2);
}
public
function
getfiles(){
if
(
file_exists
(
$this
->_filepath)){
$file
=
fopen
(
$this
->_filepath,
"r"
);
Header(
"Content-type: application/octet-stream"
);
Header(
"Accept-Ranges: bytes"
);
Header(
"Accept-Length: "
.
filesize
(
$this
->_filepath));
Header(
"Content-Disposition: attachment; filename="
.
$this
->_filename);
echo
fread
(
$file
,
filesize
(
$this
->_filepath));
$buffer
=1024;
while
(!
feof
(
$file
)) {
$file_data
=
fread
(
$file
,
$buffer
);
echo
$file_data
;
}
fclose(
$file
);
}
else
{
echo
"<script>alert('对不起,您要下载的文件不存在');</script>"
;
}
}
}