ThinkPHP5使用阿里云OSS图片上传
1、下载OSS文件放在网站根目录下(OSS文件下载地址:https://gitee.com/jth1234/oss_files.git)
2、在入口文件中加载OSS
3、config文件配置oss信息(ossKeyId、ossKeySecret、endpoint以上信息开通阿里云oss服务后可获得;bucket是创建的存储目录名)
4、上传base64图片(UploadController.php)
1 <?php 2 namespace app\api\controller; 3 use think\Controller; 4 use think\Db; 5 use OSS\OssClient; 6 use OSS\Core\OssException; 7 class UploadController extends Controller { 8 /** 9 * 上传图片到oss 10 * @param string $info base64图片 11 * 12 */ 13 public function upload_pic(){ 14 $imgData = input('info',''); 15 if($imgData){ 16 $img_len = strlen($imgData); 17 $imgData = str_replace('\n','',$imgData); 18 $imgData = str_replace('\r','',$imgData); 19 $imgData = str_replace('\t','',$imgData); 20 $imgData = str_replace(PHP_EOL,'',$imgData); 21 $imgData = str_replace('\\','',$imgData); 22 $imgData = str_replace(' ','+',$imgData); 23 24 $host = config('public.host'); 25 26 $date = date('Y-m-d'); 27 $dir = "./public/user/$date"; 28 if (!file_exists($dir)){ 29 mkdir ($dir,0777,true); 30 } 31 $img = base64_decode($imgData); 32 $uniqid_name = md5(uniqid(rand())); 33 $tmpImg = './public/user/'.$date.'/user_'.$uniqid_name.'.jpg'; 34 $tmpImg1 = '/public/user/'.$date.'/user_'.$uniqid_name.'.jpg'; 35 $a = file_put_contents($tmpImg,$img);//保存图片,返回的是字节数 36 if($a){ 37 $accessKeyId = config('oss.ossKeyId'); 38 $accessKeySecret = config('oss.ossKeySecret'); 39 // Endpoint以杭州为例,其它Region请按实际情况填写。 40 $endpoint = config('oss.endpoint'); 41 // 存储空间名称 42 $bucket= config('oss.bucket'); 43 // 文件名称 44 $object = 'user/'.$date.'/user_'.$uniqid_name.'.jpg'; 45 // <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt 46 $filePath = './public/user/'.$date.'/user_'.$uniqid_name.'.jpg'; 47 try{ 48 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); 49 $ossClient->uploadFile($bucket, $object, $filePath); 50 unlink($filePath); 51 } catch(OssException $e) { 52 $data=array('code'=>config('code.error'),'msg'=>"上传失败"); 53 return json($data); 54 } 55 $img_src = config('oss.host')."/".$object; 56 $data=array('code'=>config('code.success'),'msg'=>"上传成功",'data'=>$img_src); 57 return json($data); 58 }else{ 59 $data=array('code'=>config('code.error'),'msg'=>"上传失败"); 60 return json($data); 61 } 62 }else{ 63 $data=array('code'=>config('code.error'),'msg'=>"上传图片不能为空"); 64 return json($data); 65 } 66 } 67 /** 68 * 删除OSS上的图片 69 * @param string $info OSS上完整的图片地址 70 * 71 */ 72 public function pic_del(){ 73 $info = input('pic_addr',''); 74 if(!empty($info)){ 75 $ex = config('oss.host').'/'; 76 $arr = explode($ex,$info); 77 $accessKeyId = config('oss.ossKeyId'); 78 $accessKeySecret = config('oss.ossKeySecret'); 79 $endpoint = config('oss.endpoint'); 80 $bucket= config('oss.bucket'); 81 $object = $arr[1]; 82 try{ 83 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); 84 $ossClient->deleteObject($bucket, $object); 85 } catch(OssException $e) { 86 printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n"); 87 printf($e->getMessage() . "\n"); 88 return; 89 } 90 } 91 $data=array('code'=>config('code.success'),'msg'=>"删除成功"); 92 return json($data); 93 } 94 }