error($e->getMessage()); } $bucket = C('oss_storage.bucket'); $oss_file_path ="icon/". $return_data["savename"]; $avatar = $return_data["path"]; try { $this->multiuploadFile($ossClient,$bucket,$oss_file_path,$avatar); return true; } catch (OssException $e) { /* 返回JSON数据 */ $this->error($e->getMessage()); } } public function multiuploadFile($ossClient, $bucket,$url,$file){ //$file = __FILE__; $options = array(); try{ #初始化分片上传文件 $uploadId = $ossClient->initiateMultipartUpload($bucket, $url); //$ossClient->multiuploadFile($bucket, $url, $file, $options); } catch(OssException $e) { printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n"); printf($e->getMessage() . "\n"); return; } /* * step 2. 上传分片 */ $partSize = 5 * 1000 * 1024; $uploadFile = $file; $uploadFileSize = filesize($uploadFile); $pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize); $responseUploadPart = array(); $uploadPosition = 0; $isCheckMd5 = true; foreach ($pieces as $i => $piece) { $fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO]; $toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1; $upOptions = array( $ossClient::OSS_FILE_UPLOAD => $uploadFile, $ossClient::OSS_PART_NUM => ($i + 1), $ossClient::OSS_SEEK_TO => $fromPos, $ossClient::OSS_LENGTH => $toPos - $fromPos + 1, $ossClient::OSS_CHECK_MD5 => $isCheckMd5, ); if ($isCheckMd5) { $contentMd5 = \OSS\Core\OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos); $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5; } //2. 将每一分片上传到OSS try { $responseUploadPart[] = $ossClient->uploadPart($bucket, $url, $uploadId, $upOptions); } catch(OssException $e) { printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n"); printf($e->getMessage() . "\n"); return; } //printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n"); } $uploadParts = array(); foreach ($responseUploadPart as $i => $eTag) { $uploadParts[] = array( 'PartNumber' => ($i + 1), 'ETag' => $eTag, ); } /** * step 3. 完成上传 */ try { $ossClient->completeMultipartUpload($bucket, $url, $uploadId, $uploadParts); } catch(OssException $e) { printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n"); printf($e->getMessage() . "\n"); return; } } }