该文章由
修改而来
实现:
可以遍历上级目录,遍历二级三级目录,压缩指定文件或文件夹到指定文件夹。
压缩20多M的文件下载正常,压缩70多M的文件,下载出错,具体原因不知,大文件可以注释下载代码,改为ftp下载
list.php文件代码:
header("Content-type:text/html;charset=utf8");
require('./getfile.php');
$path = isset($_GET['path']) ? $_GET['path'] : '../..'; //遍历目录
$zippath = '../../backup/'; //压缩文件保存目录
$scandir=new traverseDir();
//$scandir->scandir($scandir->currentdir);
$scandir->scandir($path);
$scandir->currentdir;
if (isset($_POST['down_load'])){
$items=$_POST['items'];
$scandir->tozip($items,$path,$zippath);//将文件压缩成zip格式
}
//echo "当前的工作目录:".$scandir->currentdir;
echo "当前目录:".$path;
if($path != '..'){
echo '上级目录';
}
echo "
当前目录下的所有文件";
?>
名称 | 大小(KB) |
$res=$scandir->fileinfo;
foreach ($res as $k=>$v){
if (!($k=='.' || $k=='..')) {//过滤掉.和..
?>
if($v['type'] == 1){
echo '文件夹:' . $k . '';
}else{
echo $k;
}
?>
echo number_format($v[0],0);
?>
}
}
?>
全选$("#selall").click(function(){
var xz = $(this).prop("checked");//判断全选按钮的选中状态
var ck = $(".filename").prop("checked",xz); //让class名为qx的选项的选中状态和全选按钮的选中状态一致。
});
getfile.php文件代码:
require'./download.php';
/**
* 遍历目录,打包成zip格式
*/
class traverseDir{
public $currentdir;//当前目录
public $filename;//文件名
public $fileinfo;//用于保存当前目录下的所有文件名和目录名以及文件大小
public function __construct(){
$this->currentdir=getcwd();//返回当前目录
}
//遍历目录
public function scandir($filepath){
if (is_dir($filepath)){
$arr=scandir($filepath);
foreach ($arr as $k=>$v){
$v2=iconv("gb2312","utf-8",$v); // 文件或目录为中文,显示乱码,因为页面是utf-8编码,从windows获得的文件名称是gb2312编码,所以要把文件名gb2312转出utf-8编码,输出到页面。但是getfilesize($v)也用到文件名获取文件大小,如果转成utf-8编码后,就找不到文件了,所以getfilesize($v)用的还是原来的gb2312编码,这也是上面变量命名为$v2的原因。关联本页面72行
$filepath_new = $filepath . '/' . $v;
if(!is_dir($filepath_new)){
$this->fileinfo[$v2][]=$this->getfilesize($filepath_new);
$this->fileinfo[$v2]['type'] = 0;
$this->fileinfo[$v2]['path'] = $filepath_new;
}else{
$this->fileinfo[$v2][]=0;
$this->fileinfo[$v2]['type'] = 1;
$this->fileinfo[$v2]['path'] = $filepath_new;
}
}
}else {
echo "";
}
}
/**
* 返回文件的大小
* @param string $filename 文件名
* @return 文件大小(KB)
*/
public function getfilesize($fname){
return filesize($fname)/1024;
}
/**
* 压缩目录
*/
public function addFileToZip($path, $zip) {
// 判断是文件还是目录,区别对待
if(is_dir($path)){
$handler = opendir($path); //打开当前文件夹由$path指定。
while (($filename = readdir($handler)) !== false) {
if ($filename != "." && $filename != "..") { // 文件夹文件名字为'.'和‘..’,不要对他们进行操作
if (is_dir($path . "/" . $filename)) { // 如果读取的某个对象是文件夹,则递归
$this->addFileToZip($path . "/" . $filename, $zip);
} else { // 将文件加入zip对象
$zip->addFile($path . "/" . $filename);
}
}
}
@closedir($path);
}else{
$zip->addFile($path);
}
}
/**
* 压缩文件(zip格式)
*/
public function tozip($items,$path,$zippath){
$zip=new ZipArchive();
$zipname = date('YmdHis',time()).'.zip';
if (!file_exists($zippath.$zipname)){
$zip->open($zippath.$zipname,ZipArchive::CREATE | ZipArchive::OVERWRITE);//创建一个空的zip文件
for ($i=0;$i
$v2=iconv("utf-8","gb2312",$items[$i]); // 由于遍历目录的时候,把文件夹名称转换成utf-8输出到页面的,所以通过表单提交的文件名是utf-8编码的,而utf-8编码的文件名在windows中是找不到对于文件的,所以要把文件名转换成gb2312编码。关联本页面17行
$this->addFileToZip($path.'/'.$v2, $zip);
}
$zip->close();
//压缩20多M的文件下载正常,压缩70多M的文件,下载出错,具体原因不知,大文件可以注释下载代码,改为ftp下载
$dw=new download($zipname,$zippath); //下载文件
$dw->getfiles();
//echo "";
}
}
}
?>
download.php文件代码:
/**
* 下载文件
*/
class download{
protected $_filename;
protected $_filepath;
protected $_filesize;//文件大小
public function __construct($filename,$filepath){
$this->_filename=$filename;
$this->_filepath=dirname(__FILE__).'/'.$filepath.$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));
//修改之后,一次只传输1024个字节的数据给客户端
//向客户端回送数据
$buffer=1024;//
//判断文件是否读完
while (!feof($file)) {
//将文件读入内存
$file_data=fread($file,$buffer);
//每次向客户端回送1024个字节的数据
echo $file_data;
}
fclose($file);
}else {
echo "";
}
}
}
?>