/**
* Notes: 人脸数据-图片的zip压缩包上传
* Created by xxg@seabig.cn
* DateTime: 2020-01-06 15:25
*/
public function zipUploads()
{
$this->_globals();
if (!$_FILES) {
die(json_encode(array("status" => "false", "message" => "上传失败,请上传文件")));
}
//获取压缩包
$file = $_FILES['file'];
//获取文件夹名
$name = substr($file['name'], 0, strpos($file['name'], '.'));
//定义文件保存路径
$filepath = _FILE_PATH . "/face/" . date("Y") . "/" . date("m") . "/" . date("d");
//递推创建文件夹
$this->mkpath($filepath);
//移动文件
$movepath = $filepath . "/" . $file['name'];
$res = move_uploaded_file($file['tmp_name'], $movepath);
if ($res) {
//实例化ZipArchive类
$zip = new ZipArchive();
//打开压缩文件,打开成功时返回true
if ($zip->open($filepath . "/" . $_FILES['file']['name']) === true) {
//解压文件到获得的路径a文件夹下
$files = $zip->extractTo($filepath);
//关闭
$zip->close();
//移动文件
$path = _FILE_PATH . "/face/" . date("Y") . "/" . date("m") . "/" . date("d") . "/" . $name;
$arr = scandir($path);
foreach ($arr as $val) {
if (strlen($val) > 2) {
$oldfile = $filepath . "/" . $name . "/" . $val;
$newfile = $filepath . "/" . $val;
copy($oldfile, $newfile);
}
}
//删除多余文件
$this->dir($filepath . "/" . $name);
@rmdir($filepath . "/" . $name);
$this->dir($filepath."/__MACOSX");
@rmdir($filepath."/__MACOSX");
unlink($filepath . "/" . $name . ".zip"); //删除文件
die(json_encode(array("status" => "success", "message" => "上传成功")));
} else {
die(json_encode(array("status" => "false", "message" => "上传失败")));
}
}
}
/**
* Notes: 递归删除目录中的文件及文件夹
* Created by jessie@seabig.cn
* DateTime: 2020-01-06 20:16
*/
public function dir($path)
{
//扫描一个目录内的所有目录和文件并返回数组
$dirs = scandir($path);
foreach ($dirs as $dir) {
//排除目录中的当前目录(.)和上一级目录(..)
if ($dir != '.' && $dir != '..') {
//如果是目录则递归子目录,继续操作
$sonDir = $path . '/' . $dir;
if (is_dir($sonDir)) {
//递归删除
$this->dir($sonDir);
//目录内的子目录和文件删除后删除空目录
@rmdir($sonDir);
} else {
//如果是文件直接删除
@unlink($sonDir);
}
}
}
}
![]()