云起工作室 15711107967
thinkphp6文件上传相关
2022-12-30 11:39:02

thinkphp6

// 验证图片大小 和是否是图片

$res = validate(['file'=>[

'fileSize' => 410241024,

'fileExt' => 'jpg,jpeg,png,bmp,gif',

'fileMime' => 'image/jpeg,image/png,image/gif', //这个一定要加上,很重要我认为!

]])->check(['file' => $file]);



$name = $file->getOriginalName(); //上传的文件名称

$tmp_name = $file->getPathname(); //缓存路径及名称

// 图片信息

list($width, $height, $type, $attr) = getimagesize($file);


if($type==3){

$update['ext'] = ".png";

}elseif ($type==2) {

$update['ext'] = ".jpg";

}else{

$update['ext'] = ".gif";

}


$uploadPath = "uploadpath/";

上传到本地

move_uploaded_file($tmp_name, $uploadPath."/" . $name);




解压zip

$arr = array();

$unzipPath = ."unzippath/";


// 实例化对象

$zip = new \ZipArchive($file) ;

//打开zip文档,如果打开失败返回提示信息

if ($zip->open($uploadPath."/" . $name,\ZipArchive::CREATE) !== TRUE) {

return false;

}else{

//将压缩文件解压到指定的目录下

$zip->extractTo($unzipPath);

//关闭zip文档

$zip->close();

$this->getFiles($unzipPath,"",$arr);

}



// 循环路径获取base 目录下的所有文件

protected function getFiles($base,$dir,&$arr){

if(is_dir($base.$dir)){

$a=opendir($base.$dir);//打开当前目录


// //循环遍历目录内容的文件名

while(false!==($c=readdir($a))){

if($c!='.' && $c!='..'){

//判断是目录还是文件

if(is_dir($base.$dir.$c)){

$this->getFiles($base,$dir.$c."/",$arr);


}else{

$tmp['name'] = $dir.$c;

$tmp['path'] = config("app.modelHost").$base.$dir.$c;

$arr[] = $tmp;

}

}

}

}

}



//封装上传到七牛云

public function copyToQiNiu($name,$newname=""){


$conf = config("app.qiniu");


$newname = $newname?$newname:$name;


$uploadMgr = new UploadManager();

$auth = new Auth($conf['accesskey'], $conf['secretkey']);

$token = $auth->uploadToken($conf['bucket']);

list($result, $error) = $uploadMgr->putFile($token,$newname, $name);

return $result;

}