all(); $token = ''; // 获取请求头中的 token 或从参数中获取 if (request()->header('token')) { $token = request()->header('token'); } else { $token = isset($param['token']) ? $param['token'] : ''; } // 验证 token if (!$token || md5('shanxiluqiao') !== substr($token, 0, 32)) { return json(['code' => -1, 'msg' => '验证失败']); } // 设置执行时间不受限制 set_time_limit(0); // 获取上传的文件 $files = request()->file('files'); // 获取文件名称 $fileName = $param["jsonName"]; // 获取当前日期时间字符串 $currentDate = date('Y-m-d H:i:s'); // 从日期时间字符串中提取年份、月份、日期 $year = date('Y', strtotime($currentDate)); $month = date('m', strtotime($currentDate)); $day = date('d', strtotime($currentDate)); // 构建文件在 KS3 中的存储路径 $storagePath = 'proof/' . $year . '/' . $month . '/' . $day . '/' . $fileName; $uploadedFiles = []; // KS3 访问密钥、终端点、存储桶名称 $accessKey = 'AKLTo7OlaroRPiZ01LXcvOxe'; $secretKey = 'ONaWRCyzAWFjWIE4myS6cS1DnS8SkmH7YQNYN23b'; $endpoint = 'ks3-cn-beijing.ksyuncs.com'; $bucketName = 'sxlq-info'; // 创建 KS3 客户端 $ks3Client = new \Ks3Client($accessKey, $secretKey, $endpoint); // 上传文件到 KS3 foreach ($files as $index => $file) { // 构建 KS3 文件路径 $ks3FilePath = $storagePath.'/'.$index.'.'.$file->getUploadExtension(); // 上传文件到 KS3 $content = fopen($file->getPathname(), "r"); $args = array( "Bucket" => $bucketName, "Key" => $ks3FilePath, "Content"=>array( "content"=>$content, "seek_position"=>0 ), "ACL" => "public-read", "ObjectMeta" => array( "Content-Type"=>"image/jpeg", "Content-Length" => $file->getSize() ), ); $ks3Client->putObjectByFile($args); // 构建图片在 KS3 中的访问链接 $imageUrl = "http://$bucketName.$endpoint/$ks3FilePath"; $uploadedFiles[] = $imageUrl; } // 构建 JSON 文件的路径 $jsonFilePath = public_path( 'proof/' . $fileName . '.json'); // 获取 JSON 文件所在的目录路径 $jsonFileDir = dirname($jsonFilePath); // 如果目录不存在,创建目录 if (!file_exists($jsonFileDir)) { mkdir($jsonFileDir, 0777, true); } // 将图片信息保存到 JSON 文件中 file_put_contents($jsonFilePath, json_encode($uploadedFiles)); // 上传 JSON 文件到 KS3 $jsonContent = file_get_contents($jsonFilePath); $jsonArgs = [ "Bucket" => $bucketName, "Key" => 'proof/' . $fileName . '.json', "Content" => [ "content" => $jsonFilePath, "seek_position" => 0 ], "ACL" => "public-read", "ObjectMeta" => [ "Content-Type" => "application/json", "Content-Length" => strlen($jsonContent), ], ]; $ks3Client->putObjectByFile($jsonArgs); // 返回上传结果 return json(['code' => 0, 'data' => $uploadedFiles, 'msg' => '上传成功']); } }