项目介绍
laravel6百度编辑器ueditor +阿里云OSS图片或视频上传
安装教程
- 阿里云OSS sdk
- 百度编辑器
使用说明
在项目中引入必要的sdk
1、修改ueditor/php下的uploader.class.php类 修改上传文件处理的主方法 upfile
private function upFile()
{
$file = $this->file = $_FILES[$this->fileField];
if (!$file) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
return;
}
if ($this->file['error']) {
$this->stateInfo = $this->getStateInfo($file['error']);
return;
} else if (!file_exists($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
return;
} else if (!is_uploaded_file($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
return;
}
$this->oriName = $file['name'];
$this->fileSize = $file['size'];
$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;
}
//检查是否不允许的文件格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}
//创建目录失败
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];
//加载oss SDK 修改为你的文件路径(必须填写的并且一定要正确才能用,否则就会报上传错误)
include(__DIR__."/../../../vendor/aliyuncs/oss-sdk-php/autoload.php");
$accessKeyId = '**CY';
$accessKeySecret ='**ZMU';
$endpoint = 'cn-beijing.oss.aliyuncs.com';//Endpoint(地域节点)查看位置在文章下方
$bucket= '**';//" <您使用的Bucket名字,注意命名规范>";
$object = 'uediter/'.time().$this->fileType;//上传文件路径
$ossClient = new oss\OssClient($accessKeyId, $accessKeySecret, $endpoint);
try {
$ossClient->uploadFile($bucket, $object, $this->filePath, array());
$this->fullName='http://**/' . $object; //你的oss服务器地址+文件名
// 返回成功信息
$this->stateInfo = $this->stateMap[0];
} catch(oss\core\OssException $e) {
// 返回错误消息
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
}
}
}
|