来源:https://blog.csdn.net/immortalzz/article/details/81479819
事情通知:
1.你最好检验你平时上传oss的方法可行,因为网上的案例的oss方法不是通用的。
2.修改你平时上传oss时调用的$ossClient->uploadFile()的第三个参数,因为如果使用平时的$_FILES[‘file‘][‘tmp_name‘]作为文件上传路径是不可行的!你需要使用百度编辑器自定义的路径:$this->filePath。
代码:
uploader.class.php:
//找到private function upFile()
//移动文件 if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE"); } else { //移动成功 require_once(‘../../../../../vendor/aliyun/autoload.php‘); $accessKeyId = "";//去阿里云后台获取秘钥 $accessKeySecret = "";//去阿里云后台获取秘钥 $endpoint = "";//你的阿里云OSS地址 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $bucket= "";//oss中的文件上传空间 $object = ‘test/‘.md5(time()).".jpg";//想要保存文件的名称 $file = $_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName;//文件路径,必须是本地的。 try{ $ossClient->uploadFile($bucket,$object,$file); //上传成功,自己编码 // 删除本地文件 unlink($file); // 定义全局变量存储图片的oss路径 $GLOBALS[‘ossimgurl‘] = "https://".$bucket.".".$endpoint."/".$object; $this->stateInfo = $this->stateMap[0]; } catch(OssException $e) { //上传失败,自己编码 $this->stateInfo = "上传到oss失败"; } }
修改百度编辑器,修改能直接显示oss的文件:
//富文本图片路径改为阿里oss地址 public function getFileInfo() { return array( "state" => $this->stateInfo, // "url" => $this->fullName, "url" => $GLOBALS[‘ossimgurl‘], "title" => $this->fileName, "original" => $this->oriName, "type" => $this->fileType, "size" => $this->fileSize ); }
我个人的:
//移动文件 if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE"); } else { //移动成功 require_once(‘./../../../ThinkPHP/Library/Vendor/aliyun-oss/autoload.php‘); $oss = [ ‘OSS_BUCKET‘ => ‘‘, ‘OSS_KEY_ID‘ => ‘‘, ‘OSS_SECRET‘ => ‘‘, ‘OSS_HOST‘ => ‘‘, ‘OSS_FOLDER‘ => ‘‘, ‘OSS_IMG‘ => ‘‘ ]; $newFile = $_FILES[‘upfile‘]; $object = $newFile[‘name‘]; $ossClient = new OSS\OssClient($oss[‘OSS_KEY_ID‘], $oss[‘OSS_SECRET‘], $oss[‘OSS_HOST‘]); $result = $ossClient->uploadFile($oss[‘OSS_BUCKET‘], ‘goods/‘ . date(‘Ymdhis‘) . ‘/‘ . $object, $this->filePath); unlink($this->filePath); $GLOBALS[‘ossimgurl‘] = $result[‘info‘][‘url‘]; $this->stateInfo = $this->stateMap[0];}