PHP COS 视频文件上传
接着上节的文章
/**
* cos视频上传
* @param $file_url_local
* @param $file_url_server
* @param bool $isDeleteLocalFile
* @return false|string
*/
public function tencent_upload_video($file_url_local,$file_url_server,$isDeleteLocalFile=true){
$upload_success = false;
$img_url = '';
$upload_msg = '';
### 上传文件流
try {
$key = $file_url_server;
$file = fopen(PUBLIC_PATH.$file_url_local, "rb");//本地文件绝对路径
if ($file) {
$result = $this->cosClient->putObject(array(
'Bucket' => $this->bucket,
'Key' => $key,
'Body' => $file));
$data = $result->toArray() ;
if(isset($data['Location']) && isset($data['Key'])){
$img_url = $data['Key'];
$upload_success = true;
}
}
} catch (\Exception $e) {
$upload_msg = $e->getMessage();
}
//删除本地文件
if($isDeleteLocalFile) {
@unlink(PUBLIC_PATH.$file_url_local);
}
//返回信息
if($upload_success){
return json_encode(['code'=>1,'msg'=>'上传成功','url'=>$img_url]);
}
else{
return json_encode(['code'=>0,'msg'=>'上传失败','url'=>'','info'=>$upload_msg]);
}
}
使用方式
$CosSdk = new CosSdk();
$saveName = 'video'."/".date('YmdHis').uniqid().'.'.'mp4';
$CosSdk->tencent_upload_video($returnPath,$saveName,true);