//下载PHP扩展pclzip
1.composer require pclzip/pclzip
//处理中文乱码加上pclzip.lib.php
第3517行 $p_entry['filename'] = $p_path . "/" . $p_entry['filename'];//加上下面代码
2.$p_entry['filename'] = mb_convert_encoding($p_entry['filename'], 'UTF-8', 'gb2312');
/**
* 文件上传
*/
private function uploads($zipfile,$filepath,$company_id,$type){
include('./vendor/pclzip/pclzip/pclzip.lib.php');
$archive = new \PclZip($zipfile);
if ($archive->extract(PCLZIP_OPT_PATH, $filepath,
PCLZIP_OPT_REMOVE_PATH, 'install/release') == 0) {
die("Error : ".$archive->errorInfo(true));
}
$company_name = M("company")->where("id=$company_id")->getField('name');
if($type == 1){ // 毕业证书
$staff_company = "$company_name/毕业证";
}elseif ($type == 2){// 保密协议
$staff_company = "$company_name/保密协议";
}elseif($type == 3){// 劳动合同
$staff_company = "$company_name/劳动合同";
}elseif($type == 4){//身份证
$staff_company = "$company_name/身份证";
}elseif($type == 5) {//离职证明
$staff_company = "$company_name/离职证明";
}else{
$this->error('上传类型错误');
}
$file = scandir($filepath);
foreach ($file as $key=>$value){
if (!is_dir($filepath.$value)){
continue;
}
$copy = $filepath.$value;
}
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/Uploads/Staff/$staff_company";
$result = false;
$this->copy_to_file($copy,$root . $path, $result);
if ($result == true){
// 删掉临时文件
$this->deldir($filepath);
$this->success("上传成功");
}else{
$this->error("上传失败");
}
}
//获得目录下的所有文件路径并复制到指定的目录下面($old_dir:目标文件目录 $new_dir:需要复制到的文件目录,$quanxian:设置权限)
private function copy_to_file($old_dir,$new_dir,&$result=false,$quanxian=0777){
if(!is_dir($new_dir)){//判断有没有目录,没有则创建
@mkdir($new_dir,$quanxian,true);
}
$res = '';
$temp = scandir($old_dir);
if(is_array($temp) && count($temp)>2){
unset($temp[0],$temp[1]);
foreach($temp as $key=>$val){
$file_url=$old_dir.DIRECTORY_SEPARATOR.$val;
$xin_dir = $new_dir.DIRECTORY_SEPARATOR.$val;//组件新的目录
if(is_dir($file_url)){ //是否是目录
$res = $this->copy_to_file($file_url,$xin_dir,$result);
}elseif(is_file($file_url)){
$res = copy($file_url,$xin_dir);
}
if ($res == true) {
$result = true;
}
}
}
return $res;
}
//删除指定文件夹以及文件夹下的所有文件
private function deldir($dir) {
//先删除目录下的文件:
$dh = opendir($dir);
while ($file=readdir($dh)) {
if($file !="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
$this->deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}