阿裏雲上傳類
<?php
USE OSSOssClient;
USE OSSCoreOssException;
require "OssUpload/autoload.php";
Class AliUpload{
public function __construct(){
$this->init();
}
public function init(){
$CI = &get_instance();
$CI->config->load('oss', TRUE);
$config = $CI->config->item('oss','oss'); //加載oss配置
$this->Access_Key = $config['accessKeyId'];
$this->Secret_Key = $config['accessKeySecret'];
$this->bucket = $config['bucketName'];
$this->endpoint = $config['endpoint'];
}
/**
* 上傳接口
* @Author
* @DateTime 2017-03-08
* @param string $bucket bucket name
* @param string $object object name
* @param string $file local file path
* @param array $options
* @throws OssException [文件類型]
*/
public function upload($dst,$src){
//獲取對象
$auth = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
try {
//上傳圖片
$result = $auth->uploadFile($this->bucket,$dst,$src);
return $result['info']['url'];
} catch (OssException $e) {
return $e->getMessage();
}
}
/**
* @Author
* @DateTime 2018-03-08
* @return [type] [description]
*/
public function uploadVideo($dst,$src){
$ossClient = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
try{
$result = $ossClient->multiuploadFile($this->bucket,$dst,$src);
return $result['info']['url'];
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED
");
printf($e->getMessage() . "
");
return;
}
print(__FUNCTION__ . ": OK" . "
");
}
}
oss上傳圖片
/**
* 上傳圖片
* @Author
* @DateTime 2018-03-10
* @return [json] [圖片url]
*/
public function uploadImg(){
$scr = $_FILES['file']['tmp_name'];
$ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上傳文件後綴
$dst = md5(time()).'-'.$scr.'.'.$ext; //上傳文件名稱
$this->load->library('AliUpload');
$url = $this->aliupload->upload($dst,$scr);
$data = array('url' =>$url);
$this->response(0,'上傳成功',$data);
}