百度的Ueditor上传到七牛,oss等第三方存储的通用方法(php版)
Posted 杰哥之家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度的Ueditor上传到七牛,oss等第三方存储的通用方法(php版)相关的知识,希望对你有一定的参考价值。
用百度的Ueditor上传图片默认保存到本地,在分布式系统中使用非常不方便,一般需要上传到七牛,阿里云的oss上
需要七牛sdk 6.1.9(支持php5.2,高版本可自行改正)
Ueditor 1.4.3
主要原理:百度ueditor上传文件时会创建本地文件,并返回文件路径 $this->fullName,我们需要修改上传类,在上传成功时进行文件的转存,转存成功后把本地的文件删除,该方法可以处理图片,附件等小文件,大文件不建议在ueditor上上传。
分享地址:http://pan.baidu.com/s/1o8noPdg ;欢迎大家下载和共同研究
提供修改方式:(oss只要修改配置文件和上传方法就行^_^)
1.七牛官网下载sdk6.1.9,放在php的文件夹下
2.在ueditor/php下qiniu_config.php
<?php return array( "bucket"=>"***", "accessKey"=>"****", "secretKey"=>"****" ); ?>
3.修改php下的config.json,把图片访问的前缀改成相应的远程访问的前缀
"imageUrlPrefix": "", /* 图片访问路径前缀 */
4.修改主要的上传文件Uploader.class.php,将upFile方法改成如下方法
/** * 上传到七牛 * @return mixed */ 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 { //移动成功 //执行到这里,代表已经完成了上传,此时系统会提示上传成功 //------------------------------------------------ start --------------------------------------------------------- //上传到七牛 require_once("qiniu/io.php"); require_once("qiniu/rs.php"); $config = require_once("qiniu_config.php"); $bucket = $config[‘bucket‘]; //仓库名 //截取原始文件后缀名 //$key1 = "Uploads/".time().mt_rand(10,99).$this->getFileExt(); $accessKey = $config[‘accessKey‘]; $secretKey = $config[‘secretKey‘]; Qiniu_SetKeys($accessKey, $secretKey); $putPolicy = new Qiniu_RS_PutPolicy($bucket); $upToken = $putPolicy->Token(null); $putExtra = new Qiniu_PutExtra(); $putExtra->Crc32 = 1; //$file[ "tmp_name" ] ueditor上传的临时文件路径 list($ret, $err) = Qiniu_PutFile($upToken,$this->fullName,$_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName, $putExtra); //成功了,删除本地文件 if($ret) { @unlink($_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName); //file_put_contents("./log/log.txt"," time:".time()." ".json_encode($ret)." ",FILE_APPEND); } if($err) { if(!is_dir("./log")){ if(!mkdir("log",0755,true)){ die("当前目录没有写权限"); } } file_put_contents("./log/log.txt"," time:".time()." ".json_encode($_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName)." ",FILE_APPEND); $this->stateInfo = $this->getStateInfo("QINIU_ERR"); }else{ $this->stateInfo = $this->stateMap[0]; } //------------------------------------------------- end --------------------------------------------------------- } }
5.修改upBase64方法(如果不用截屏和涂鸦可以不改)
/** * 处理base64编码的图片上传 * @return mixed */ private function upBase64() { $base64Data = $_POST[$this->fileField]; $img = base64_decode($base64Data); $this->oriName = $this->config[‘oriName‘]; $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; } //创建目录失败 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 { //移动成功 require_once("qiniu/io.php"); require_once("qiniu/rs.php"); $config = require_once("qiniu_config.php"); $bucket = $config[‘bucket‘]; //仓库名 //截取原始文件后缀名 //$key1 = "Uploads/".time().mt_rand(10,99).$this->getFileExt(); $accessKey = $config[‘accessKey‘]; $secretKey = $config[‘secretKey‘]; Qiniu_SetKeys($accessKey, $secretKey); $putPolicy = new Qiniu_RS_PutPolicy($bucket); $upToken = $putPolicy->Token(null); $putExtra = new Qiniu_PutExtra(); $putExtra->Crc32 = 1; //$file[ "tmp_name" ] ueditor上传的临时文件路径 list($ret, $err) = Qiniu_PutFile($upToken,$this->fullName,$_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName, $putExtra); if($ret) { //删临时文件 @unlink($_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName); // file_put_contents("./log/log.txt"," time:".time()." ".json_encode($ret)." ",FILE_APPEND); } if($err) { file_put_contents("./log/log.txt"," time:".time()." ERR:".json_encode($err)." ".json_encode($_SERVER[‘DOCUMENT_ROOT‘]."/".$this->fullName)." ",FILE_APPEND); // $this->stateInfo = $this->getStateInfo("QINIU_ERR"); } $this->stateInfo = $this->stateMap[0]; } }
转载自: https://my.oschina.net/u/2528821/blog/817896
以上是关于百度的Ueditor上传到七牛,oss等第三方存储的通用方法(php版)的主要内容,如果未能解决你的问题,请参考以下文章
ueditor上传图片到七牛云存储(form api,java)
ueditor1.4.3 php如何上传图片到图片服务器(七牛)