tp6上传zip格式压缩包并解压

 public function upload()
    {
        $file = request()->file('file');

        try {
            // 验证文件格式
            validate(['file' => ['fileExt' => 'zip', 'fileMime' => 'application/zip']])->check(['file' => $file]);
            // 移动到框架应用根目录/public/uploads/ztzlzip 目录下
            $info = \think\facade\Filesystem::disk('public')->putFile('zip', $file);
            // 拼接上传后的文件绝对路径
            $uploadPath = str_replace('\\', '/', './uploads/' . $info);
            // 定义解压路径
            $unzipPath = './uploads/ztzl/';
            // 实例化对象
            $zip = new \ZipArchive();
            //打开zip文档,如果打开失败返回提示信息
            if ($zip->open($uploadPath, \ZipArchive::CREATE) !== TRUE) {
                die ("Could not open archive");
            } else {
                //将压缩文件解压到指定的目录下
                $zip->extractTo($unzipPath);
                //关闭zip文档
                $zip->close();
                $msg['code'] = 0;
                $msg['path'] = 'http://' . $_SERVER['SERVER_NAME'] . $unzipPath;
            }

        } catch (ValidateException $e) {
            $this->error($e->getError());
            $msg['code'] = 1;
            $msg['info'] = $e->getError();
        }
        return json($msg);
    }

 

你可能感兴趣的:(php,thinkphp6)