'上传内容或者文件格式有误']; // 获得用户路径 $user_path = $this->_get_user_path($action); // 如果不存在目录 if( !is_dir($user_path) ) mkdir($user_path,0755,true); // 文件名称 $filename = $user_path.uniqid().'.'. $ext; // 写入数据 $result = @file_put_contents($filename,$content); // 失败提示 if( !$result ) return ['error'=>'上传失败']; // 返回路径 return request()->root().'/'.$filename; } /** * 上传 */ public function upload() { // 接收参数 $this->type = request('edit_type',''); $action = request('action'); // 获取配置 $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./static/ueditor/config.json")), true); // 赋值通用 $this->config = $CONFIG; // 判断方法,调用对应方式 switch ($action) { case 'config': $result = response()->json($CONFIG); break; /* 上传图片 */ case 'uploadimage': $result = $this->_ueditor_upload(['size' => 1024*1024*100,'ext'=>['jpg', 'gif', 'png', 'jpeg']],'images'); break; /* 上传涂鸦 */ case 'uploadscrawl': $result = $this->_ueditor_upload_scrawl('scrawl'); break; /* 上传视频 */ case 'uploadvideo': $result = $this->_ueditor_upload(['size' => 1024*1024*100 ,'ext'=>['mp4', 'avi', 'wmv','rm','rmvb','mkv']],'video'); break; /* 上传文件 */ case 'uploadfile': $result = $this->_ueditor_upload(['ext'=>['txt','pdf','doc','docx','xls','xlsx','zip','rar','ppt','pptx']],'file'); break; /* 列出图片 */ case 'listimage': $result = $this->_ueditor_list('images'); break; /* 列出文件 */ case 'listfile': $result = $this->_ueditor_list('file'); break; /* 抓取远程文件 */ case 'catchimage': $result = $this->_ueditor_upload_catch(); break; default: $result = response()->json(['state'=>'请求地址错误']); break; } // 返回结果 return $result; } /** * 编辑列表 * * @param String $action 方法 * * * */ private function _ueditor_list($action) { // 配置 $config = $this->config; // 判断文件后缀 $ext_allow = $config['imageAllowFiles']; // 默认图片列表长度 $listSize = $this->config['imageManagerListSize']; /* 判断类型 */ if( $action == 'file' ) { // 如果是文件 $listSize = $this->config['fileManagerListSize']; // 判断文件后缀 $ext_allow = $config['fileAllowFiles']; } // 允许的文件对象 foreach ($ext_allow as $key => $value) { // 去除点 $ext_allow[$key] = ltrim($value,'.'); } // 合并字符串 $ext_allow = implode(',',$ext_allow); // 列表长度 $size = request('size',$listSize); // 开始 $start = request('start',0); // 获得用户路径 $user_path = $this->_get_user_path($action); // 取得文件 $files = (array) glob($user_path.'*.{'.$ext_allow.'}', GLOB_BRACE); // 循环处理 foreach ($files as $key => $value) { // 组装路径 $files[$key] = request()->root().'/'.$value; } // 总数量 $total = count($files); // 列表数据 $list = array_slice($files,$start,$size); // 返回数据 return response()->json(['state'=>'SUCCESS','list'=>$list,'start'=>$start,'total'=>$total]); } /** * 用户目录 * * */ private function _get_user_path($action){ // 当前登录用户 if( admin('uid') ) return $this->_basepath.$action.'/admin/'.admin('uid').'/'.date('Ymd').'/'; } /** * 上传 * */ private function _ueditor_upload($config=[],$action='') { // 如果没有文件 if( !$action ) return response()->json(['state'=>'上传方法有误','url'=>'','title'=>'','original'=>'']); // 如果没有文件 if( !request()->hasFile('upfile') ) return response()->json(['state'=>'未接收到文件','url'=>'','title'=>'','original'=>'']); // 本地上传 $file = request()->file('upfile'); // 如果没有文件 if( !$file->isValid() ) return response()->json(['state'=>'上传错误','url'=>'','title'=>'','original'=>'']); // 配置合并配置 if( !empty($config) ) $config = array_merge($this->config,$config); // 获取文件后缀名 $ext = $file->extension(); // 获取文件大小 $size = $file->getSize(); // 验证文件大小 if( isset($config['size']) && $size >= $config['size'] ) return response()->json(['state'=>'文件大小超过限制大小','url'=>'','title'=>'','original'=>'']); // 验证文件格式 if( isset($config['ext']) && !in_array($ext,$config['ext'] ) ) return response()->json(['state'=>'请上传允许的格式','url'=>'','title'=>'','original'=>'']); // 获取文件名 $filename = ltrim($file->getClientOriginalName(),'/'); // 文件名 $filename = html_entity_decode($filename, ENT_QUOTES, 'UTF-8'); // 获得用户路径 $user_path = $this->_get_user_path($action); // 保存文件 $info = $file->move($user_path,$filename); // 如果有错误 if( !$info ) return ['error'=>$file->getError()]; // 移动失败 if( isset($objectPath['error']) ) return response()->json(['state'=>$objectPath['error'],'url'=>'','title'=>'','original'=>'']); // 数据 $url = request()->root().'/'.$user_path.$filename; // 状态成功 return response()->json(['state' => 'SUCCESS','url'=>$url,'title'=>$filename,'original'=>$filename]); } /** * 涂鸦 * */ private function _ueditor_upload_scrawl($action='scrawl'){ // 接收数据 $data = request($this->config['scrawlFieldName'],''); // 未接收到涂鸦数据 if ( empty ($data) ) return response()->json(['state'=>'涂鸦内容不能为空','url'=>'','title'=>'','original'=>'']); // base64转码接收到的涂鸦数据 $img = base64_decode($data); // 大小 $size = strlen($img); // 验证文件大小 if( isset($this->config['scrawlMaxSize']) && $size >= $this->config['scrawlMaxSize'] ) return response()->json(['state'=>'文件大小超过限制大小','url'=>'','title'=>'','original'=>'']); // 调用本地存储方法 $savepath = $this->save_storage_content($action,'png',$img); // 存储失败 if ( isset($savepath['error']) ) return response()->json(['state'=>$savepath['error'],'url'=>'','title'=>'','original'=>'']); // 状态成功 return response()->json(['state' => 'SUCCESS','url'=>$savepath,'title'=>'','original'=>'']); } //抓取远程文件 private function _ueditor_upload_catch(){ // 暂不支持 return response()->json(['state' => '暂不支持抓取远程文件','list' => null]); } }