首先下载ueditor文件编辑器 http://ueditor.baidu.com/website/download.html
将下载的ueditor解压后放到laravel框架下的public文件中
我放在了这
下面是具体内容操作和修改
第一步新建文件:OssInUe.php (位置:public/vendor/laravel-u-editor/php/)
<?php if (is_file('../../../../vendor/autoload.php')) { require_once '../../../../vendor/autoload.php'; } use OSS\OssClient; use OSS\Core\OssException; /** * Created by PhpStorm. * User: crjy * Date: 2017/10/13 * Time: 15:46 */ class OssInUe{ public function __construct(){ } function uploadToAliOSS($file,$fileType){ $entension = $fileType; //上传文件的后缀 $newName = date('YmdHis').mt_rand(100000,999999).$entension;//上传到oss的文件名 文件重命名 $accessKeyId = '阿里云id'; $accessKeySecret ='阿里云key'; $endpoint = '你的端点';//域名,端点名 $bucket= '你的bucket名';//" <您使用的Bucket名字,注意命名规范>"; $object = '上传的路径'.$newName;//" <您使用的Object名字,注意命名规范>"; (可以写到.env文件中调用) $content = $file["tmp_name"];//上传的文件 try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,false); $ossClient->setTimeout(3600); /* seconds */ $ossClient->setConnectTimeout(10); /* seconds */ // $ossClient->putObject($bucket, $object, $content); // 先把本地的example.jpg上传到指定$bucket, 命名为$object $ossClient->uploadFile($bucket, $object, $content); $signedUrl = $ossClient->signUrl($bucket, $object); $path = explode('?',$signedUrl)[0]; $obj['status'] = true; $obj['path'] = $path; } catch (OssException $e) { $obj['status'] = false; $obj['path'] = ""; print $e->getMessage(); } return $obj; } }
第二步修改文件:Uploader.class.php (位置:public/vendor/laravel-u-editor/php/)
<?php require_once __DIR__.'/OssInUe.php'; /** * Created by JetBrains PhpStorm. * User: taoqili * Date: 12-7-18 * Time: 上午11: 32 * UEditor编辑器通用上传类 */ class Uploader { /** * 构造函数 * @param string $fileField 表单名称 * @param array $config 配置项 * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名 */ public function __construct($fileField, $config, $type = "upload") { /* 代码太多删了 */ } /** * 上传文件的主处理方法 * @return mixed */ private function upFile() { /* 代码太多删了 */ //检查文件大小是否超出限制 if (!$this->checkSize()) { $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED"); return; } //检查是否不允许的文件格式 if (!$this->checkType()) { $this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED"); return; } /*支持阿里云OSS修改:第一处,start*/ //创建目录失败 /*if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) { $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR"); return; } else if (!is_writeable($dirname)) { $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE"); return; }*/ //移动文件 /*if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE"); } else { //移动成功 $this->stateInfo = $this->stateMap[0]; }*/ $ossInUe = new OssInUe(); $obj = $ossInUe->uploadToAliOSS($file,$this->fileType); if ($obj['status'] == true){ $this->fullName = $obj['path']; $this->stateInfo = $this->stateMap[0]; }else{ $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT"); } /*支持阿里云OSS修改:第一处,end*/ } /** * 处理base64编码的图片上传 * @return mixed */ private function upBase64() { /* 代码太多删了 */ //检查文件大小是否超出限制 if (!$this->checkSize()) { $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED"); return; } /*支持阿里云OSS修改:第二处,start*/ /*//创建目录失败 if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) { $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR"); return; } else if (!is_writeable($dirname)) { $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE"); return; }*/ //移动文件 /*if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT"); } else { //移动成功 $this->stateInfo = $this->stateMap[0]; }*/ $ossInUe = new OssInUe(); $obj = $ossInUe->uploadToAliOSS($img,$this->fileType); if ($obj['status'] == true){ $this->fullName = $obj['path']; $this->stateInfo = $this->stateMap[0]; }else{ $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT"); } /*支持阿里云OSS修改:第二处,end*/ } /** * 拉取远程图片 * @return mixed */ private function saveRemote() { /* 代码太多删了 */ //打开输出缓冲区并获取远程图片 ob_start(); $context = stream_context_create( array('http' => array( 'follow_location' => false // don't follow redirects )) ); readfile($imgUrl, false, $context); $img = ob_get_contents(); ob_end_clean(); preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m); $this->oriName = $m ? $m[1]:""; $this->fileSize = strlen($img); $this->fileType = $this->getFileExt(); $this->fullName = $this->getFullName(); $this->filePath = $this->getFilePath(); $this->fileName = $this->getFileName(); $dirname = dirname($this->filePath); //检查文件大小是否超出限制 if (!$this->checkSize()) { $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED"); return; } /*支持阿里云OSS修改:第三处,start*/ /* //创建目录失败 if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) { $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR"); return; } else if (!is_writeable($dirname)) { $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE"); return; } //移动文件 if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT"); } else { //移动成功 $this->stateInfo = $this->stateMap[0]; }*/ $ossInUe = new OssInUe(); $obj = $ossInUe->uploadToAliOSS($img,$this->fileType); if ($obj['status'] == true){ $this->fullName = $obj['path']; $this->stateInfo = $this->stateMap[0]; }else{ $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT"); } /*支持阿里云OSS修改:第三处,start*/ } /** * 上传错误检查 * @param $errCode * @return string */ private function getStateInfo($errCode) { return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode]; } /** * 获取文件扩展名 * @return string */ private function getFileExt() { return strtolower(strrchr($this->oriName, '.')); } /* 后面代码太多没有修改 删了 */ }