php-tp5实现上传文件夹到服务器

/* 这个是给前端的api接口 * */
public function index(){
        if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
            header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
         return 200;
        }
        $date = input('date');  //接收前端传值过来的文件唯一标识符
        $arr = explode('/',input('dir'));  //分割传值来的本地地址
        $arr[1] .= '-'.$date;      //连接地址和唯一标识符
        $driStr = implode('/',$arr);  //转化
        $files = $_FILES;    //接收文件
        $dir = ROOT_PATH . 'public' . DS . 'worker/'.$driStr;   //存放服务器地址
        if (!file_exists($dir)) {
            mkdir($dir, 0700,true);
        }else {
            $imageArr = Array();
            foreach ($files as $file) {
                $imageName = $file['name'];
                $tmp = $file['tmp_name'];
                $imageSavePath = $dir.'/'.$imageName;
                $info = move_uploaded_file($tmp,$imageSavePath);
                if ($info) {
                    $path = '/'.$dir.'/'.$imageSavePath;
                    array_push($imageArr,$path);
                }
            } 
            //最终生成的字符串路径
            $imagePathStr = implode(',',$imageArr);
            return json(['code'=>200,'msg'=>$imagePathStr]);
        }
        $imageArr = Array();
        foreach ($files as $file) {
            $imageName = $file['name'];
            $tmp = $file['tmp_name'];
            $imageSavePath = $dir.'/'.$imageName;
            $info = move_uploaded_file($tmp,$imageSavePath);
            if ($info) {
                 $path = '/'.$dir.'/'.$imageSavePath;
                array_push($imageArr,$path);
            }
        } 
        //最终生成的字符串路径
        $imagePathStr = implode(',',$imageArr);
        return json(['code'=>200,'msg'=>'上传成功']);
	  }
}
没有账号? 忘记密码?

社交账号快速登录