如题。
前端部分:
var ue = UE.getEditor('container', {
serverUrl:'{pigcms::$f_siteUrl}<?php echo U("Home/Upload/UeditorUpload"); ?>'
});
ue.ready(function() {
ue.execCommand('serverparam', {
'token': '{pigcms::$token}',
'isTuwen':1,
});
});
后端部分:
class UploadAction extends BaseAction {
public $token;
public function _initialize() {
parent::_initialize();
$this->token=$this->_session('token');
if (!$this->token){
$this->token='admin';
}
}
/**
* 自定义ueditor文件上传
*/
public function UeditorUpload(){
switch($_GET['action']){
case 'config':
$config = C('ueditor_config');
echo json_encode($config);
exit;
break;
case 'uploadimage':
if($_GET['isTuwen'] == 1){
$token = $_GET['token'];
$wxuser = M('Wxuser')->where(array('token'=>$token))->find();
if(!empty($wxuser)){
import('ORG.Net.UploadFile');
$upload = new UploadFile();// 实例化上传类
$upload->maxSize = intval(C('up_size'))*1024;// 设置附件上传大小
$upload->allowExts = explode(',',C('up_exts'));// 设置附件上传类型
$upload->savePath = C('up_path');// 设置附件上传目录
if(!$upload->upload()) {// 上传错误提示错误信息
$re = array();
$re['state'] = $upload->getErrorMsg();
echo json_encode($re);
exit;
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
$pic_path = $info['0']['savepath'].$info['0']['savename'];
$data['media']='@'.realpath($pic_path);
$weouth_new = new Weouth_new($wxuser['appid']);
$result = $weouth_new->uploadForeverMedia($data, 'image');
unlink(realpath($pic_path));//删除已上传的图片
if($result){
$re = array();
$re['state'] = "SUCCESS";
$re['url'] = $result['url'];
$re['title'] = $_FILES['upfile']['name'];
$re['original'] = $result['url'];
}else{
$re = array();
$re['state'] = $weouth_new->errMsg;
}
}
echo json_encode($re);
exit;
}else{
$return=$this->localUpload();
if($return['error']){
$re = array();
$re['state'] = $return['msg'];
}else{
$re = array();
$re['state'] = "SUCCESS";
$re['url'] = $return['msg'];
$re['title'] = $_FILES['upfile']['name'];
$re['original'] = $return['msg'];
}
echo json_encode($re);
exit;
}
break;
}
}
public function upload(){
if (!function_exists('imagecreate')){
exit('php不支持gd库,请配置后再使用');
}
if (IS_POST){
$return=$this->localUpload();
echo "";
echo("");
exit();
}
}
/**
* 线上上传方法
*/
public function uploadImg(){
$return=$this->localUpload();
if ($return['error']){
$this->alert($return['msg']);
}else {
header('Content-type: text/html; charset=UTF-8');
echo json_encode(array('status' => 1, 'path' => $return['msg'],'small_path'=>$return['small'],'filename'=>$return['filename']));
exit;
}
}
/**
* 线上上传音乐
*/
public function uploadMusic(){
$return=$this->localUpload('mp3');
if ($return['error']){
$this->alert($return['msg']);
}else {
header('Content-type: text/html; charset=UTF-8');
echo json_encode(array('status' => 1, 'path' => $return['msg'],'small_path'=>$return['small'],'filename'=>$return['filename']));
exit;
}
}
public function localUpload($filetypes=''){
$file_input_name="";
$file_info=array();
foreach ($_FILES as $key=>$file){
if(!empty($file['name'])) {
$file_input_name=$key;
$file_info['size']=$file['size'];
$pathinfo = pathinfo($file['name']);
$file_info['extension']=$pathinfo['extension'];
break;
}
}
if($file_input_name){
$uf = new UploadFile_new($file_input_name);
$uf->setMaxSize(intval(C('up_size'))) ;//KB
if (!$filetypes){
$upload_allowExts = implode("|",explode(',',C('up_exts')));
}else {
$upload_allowExts = $filetypes;
}
$uf->setFileType($upload_allowExts);
if (isset($_POST['width'])&&intval($_POST['width'])){
$isneed_Resize=true;
$uf->setResizeImage(true);
$uf->setResizeWidth($_POST['width']);
$uf->setResizeHeight($_POST['height']);
}
$uf->setUploadType("ftp");//设置上传方式,默认为程序上传
$uf->setShowAsChinese(true);
if(($rtnMSG=$uf->upload())=="success")
{
$error=0;
if($isneed_Resize){
$msg = $uf->getResizeImageURL();
}else{
$msg = $uf->getSaveFileURL();
}
$filename=$uf->getFileName();
$small_img_URL= $uf->getResizeImageURLForMobile();
}else{
$error=1;
$msg=$rtnMSG;
$small_img_URL='';
}
}else{
$error=1;
$small_img_URL='';
$msg="请选择图片";
}
return array('error'=>$error,'msg'=>$msg,'small'=>$small_img_URL,'filename'=>$filename);
}
public function alert($msg) {
header('Content-type: text/html; charset=UTF-8');
echo json_encode(array('error' => 1, 'message' => $msg));
exit;
}
/**
* 线下上传图片
*/
public function uploadImg_test(){
$path = '';
$result = array();
import('@.ORG.Util.Upload');
$uf = new Upload('Filedata');//file
$uf->setMaxSize(102400);
$uf->setUploadType("ftp");
$uf->setServerSite('20');
$uf->setSaveDir("/klf/users/album/");
$uf->setResizeReality(true);
$rtnMSG=$uf->doupload();
if($rtnMSG=="success"){
$path=$uf->getSaveFileURL();
$filename=$uf->getFileName();
//wss 获取上传名称
$result = array('status'=>"1",'path'=>$path,'filename'=>$filename);
echo json_encode($result);
die();
}else{
$result = array('status'=>"0",'path'=>$path);
echo json_encode($result);
die();
}
}
}
本文介绍了如何将UEditor编辑器与后端PHP结合,实现图片上传功能。当图片上传时,若设置为图文模式,会调用微信API将图片上传到微信服务器作为永久素材,否则则保存在本地服务器。涉及的技术包括UEditor、微信接口、文件上传处理及错误处理。
2369

被折叠的 条评论
为什么被折叠?



