驻足生活 2022-04-28 17:29 采纳率: 33.3%
浏览 308
已结题

百度编辑器ueditor全网搜不到解决方案的问题,ueditor配置文件中php/controller.php一个接口文件,请求没有返回,竟然下载了。

thinkphp5开发的cms系统,部署在百度虚拟主机nginx操作系统

今天把程序放在了百度虚拟主机上,访问前后台都正常,就是ueditor配置出了问题,ueditor下面有个php/controller.php文件,这个文件其实是个返回数据的文件(如图)

img


正常之前放在西部数码上的,请求这个文件(链接为:http://www.0000.com/static/admin/plugins/ueditor/php/controller.php?action=config)返回的是一个json(如图

img

但是刚在百度虚拟主机上,访问同个链接竟然自动下载controller.php(如图)

img

,所以这个问题一直导致报错信息后台配置项返回格式出错,上传功能将不能正常使用!,我搜了一下好像是nginx操作系统不识别.php,但是其他.php文件都可以访问,就是ueditor的这个.php没有,网上找了一天了。没找到相关问题的解决方法,请求给解答一下?急啊

  • 写回答

2条回答 默认 最新

  • 驻足生活 2022-04-29 16:43
    关注

    已经解决,由于是nginx操作系统,存在拦截器读取.php文件失败。想到另一个办法,在ueditor配置文件的php/controller.php,移到正常项目的路径,本来这就是个返回接口的文件,我是TP5。在admin/contrlooer新建Ueditor.php,把controller.php复制放在里面,注意修改namespace之类的。再次访问就可以正常访问返回json了。然后这个文件就作为编辑器上传文件的接口了,修改里面的一下验证size,后缀的,自己去写就行了。在初始化ueditor编辑器的时候,增加一个参数

    serverUrl:"{:url('Ueditor/index')}",
    
    

    img

    就行了。
    注意有个config.json的文件,用它的绝对路径就行了。
    下面是ueditor.php,上部(在admin/contrlooer新建Ueditor.php)

    <?php
    namespace app\admin\controller;
    use think\Controller;
    use think\Image;
    use think\Request;
    
    class Ueditor extends Controller{
        public function index(){
            //header('Access-Control-Allow-Origin: http://www.baidu.com'); //设置http://www.baidu.com允许跨域访问
            //header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //设置允许的跨域header
            date_default_timezone_set("Asia/Chongqing");
            error_reporting(E_ERROR);
            header("Content-Type: text/html; charset=utf-8");
            $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($_SERVER['DOCUMENT_ROOT']."/public/static/admin/plugins/ueditor/php/config.json")), true);
            $action = $_GET['action'];
    
            switch ($action) {
                case 'config':
                    $result =  json_encode($CONFIG);
                    break;
                /* 上传图片 */
                case 'uploadimage':
                    $fieldName = $CONFIG['imageFieldName'];
                    $result = $this->upFile($fieldName);
                    break;
                /* 上传涂鸦 */
                case 'uploadscrawl':
                    $config = array(
                        "pathFormat" => $CONFIG['scrawlPathFormat'],
                        "maxSize" => $CONFIG['scrawlMaxSize'],
                        "allowFiles" => $CONFIG['scrawlAllowFiles'],
                        "oriName" => "scrawl.png"
                    );
                    $fieldName = $CONFIG['scrawlFieldName'];
                    $base64 = "base64";
                    $result = $this->upBase64($config,$fieldName);
                    break;
                /* 上传视频 */
                case 'uploadvideo':
                    $fieldName = $CONFIG['videoFieldName'];
                    $result = $this->upFile($fieldName);
                    break;
                /* 上传文件 */
                case 'uploadfile':
                    $fieldName = $CONFIG['fileFieldName'];
                    $result = $this->upFile($fieldName);
                    break;
                /* 列出图片 */
                case 'listimage':
                    $allowFiles = $CONFIG['imageManagerAllowFiles'];
                    $listSize = $CONFIG['imageManagerListSize'];
                    $path = $CONFIG['imageManagerListPath'];
                    $get =$_GET;
                    $result =$this->fileList($allowFiles,$listSize,$get);
                    break;
                /* 列出文件 */
                case 'listfile':
                    $allowFiles = $CONFIG['fileManagerAllowFiles'];
                    $listSize = $CONFIG['fileManagerListSize'];
                    $path = $CONFIG['fileManagerListPath'];
                    $get = $_GET;
                    $result = $this->fileList($allowFiles,$listSize,$get);
                    break;
                /* 抓取远程文件 */
                case 'catchimage':
                    $config = array(
                        "pathFormat" => $CONFIG['catcherPathFormat'],
                        "maxSize" => $CONFIG['catcherMaxSize'],
                        "allowFiles" => $CONFIG['catcherAllowFiles'],
                        "oriName" => "remote.png"
                    );
                    $fieldName = $CONFIG['catcherFieldName'];
                    /* 抓取远程图片 */
                    $list = array();
                    isset($_POST[$fieldName]) ? $source = $_POST[$fieldName] : $source = $_GET[$fieldName];
    
                    foreach($source as $imgUrl){
                        $info = json_decode($this->saveRemote($config,$imgUrl),true);
                        array_push($list, array(
                            "state" => $info["state"],
                            "url" => $info["url"],
                            "size" => $info["size"],
                            "title" => htmlspecialchars($info["title"]),
                            "original" => htmlspecialchars($info["original"]),
                            "source" => htmlspecialchars($imgUrl)
                        ));
                    }
    
                    $result = json_encode(array(
                        'state' => count($list) ? 'SUCCESS':'ERROR',
                        'list' => $list
                    ));
                    break;
                default:
                    $result = json_encode(array(
                        'state' => '请求地址出错'
                    ));
                    break;
            }
    
            /* 输出结果 */
            if(isset($_GET["callback"])){
                if(preg_match("/^[\w_]+$/", $_GET["callback"])){
                    echo htmlspecialchars($_GET["callback"]).'('.$result.')';
                }else{
                    echo json_encode(array(
                        'state' => 'callback参数不合法'
                    ));
                }
            }else{
                echo $result;
            }
        }
    
        //上传文件
        private function upFile($fieldName){
            $file = request()->file($fieldName);
            $info = $file->move('upload');
            if($info){//上传成功
                $fname='/upload/'.str_replace('\\','/',$info->getSaveName());
    
                $imgArr = explode(',', 'jpg,gif,png,jpeg,bmp,ttf,tif');
                $imgExt= strtolower($info->getExtension());
    //增加验证参数,大小后缀啥的
                $data=array(
                    'state' => 'SUCCESS',
                    'url' => $fname,
                    'title' => $info->getFilename(),
                    'original' => $info->getFilename(),
                    'type' => '.' . $info->getExtension(),
                    'size' => $info->getSize(),
                );
            }else{
                $data=array(
                    'state' => $info->getError(),
                );
            }
            return json_encode($data);
        }
    
        //列出图片
        private function fileList($allowFiles,$listSize,$get){
            $dirname = './public/uploads/';
            $allowFiles = substr(str_replace(".","|",join("",$allowFiles)),1);
    
            /* 获取参数 */
            $size = isset($get['size']) ? htmlspecialchars($get['size']) : $listSize;
            $start = isset($get['start']) ? htmlspecialchars($get['start']) : 0;
            $end = $start + $size;
    
            /* 获取文件列表 */
            $path = $dirname;
            $files = $this->getFiles($path,$allowFiles);
            if(!count($files)){
                return json_encode(array(
                    "state" => "no match file",
                    "list" => array(),
                    "start" => $start,
                    "total" => count($files)
                ));
            }
    
            /* 获取指定范围的列表 */
            $len = count($files);
            for($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
                $list[] = $files[$i];
            }
    
            /* 返回数据 */
            $result = json_encode(array(
                "state" => "SUCCESS",
                "list" => $list,
                "start" => $start,
                "total" => count($files)
            ));
    
            return $result;
        }
    
        /*
      * 遍历获取目录下的指定类型的文件
      * @param $path
      * @param array $files
      * @return array
     */
        private function getFiles($path,$allowFiles,&$files = array()){
            if(!is_dir($path)) return null;
            if(substr($path,strlen($path)-1) != '/') $path .= '/';
            $handle = opendir($path);
    
            while(false !== ($file = readdir($handle))){
                if($file != '.' && $file != '..'){
                    $path2 = $path.$file;
                    if(is_dir($path2)){
                        $this->getFiles($path2,$allowFiles,$files);
                    }else{
                        if(preg_match("/\.(".$allowFiles.")$/i",$file)){
                            $files[] = array(
                                'url' => substr($path2,1),
                                'mtime' => filemtime($path2)
                            );
                        }
                    }
                }
            }
    
            return $files;
        }
    
        //抓取远程图片
        private function saveRemote($config,$fieldName){
            $imgUrl = htmlspecialchars($fieldName);
            $imgUrl = str_replace("&","&",$imgUrl);
    
            //http开头验证
            if(strpos($imgUrl,"http") !== 0){
                $data=array(
                    'state' => '链接不是http链接',
                );
                return json_encode($data);
            }
            //获取请求头并检测死链
            $heads = get_headers($imgUrl);
            if(!(stristr($heads[0],"200") && stristr($heads[0],"OK"))){
                $data=array(
                    'state' => '链接不可用',
                );
                return json_encode($data);
            }
            //格式验证(扩展名验证和Content-Type验证)
            $fileType = strtolower(strrchr($imgUrl,'.'));
            if(!in_array($fileType,$config['allowFiles']) || stristr($heads['Content-Type'],"image")){
                $data=array(
                    'state' => '链接contentType不正确',
                );
                return json_encode($data);
            }
    
            //打开输出缓冲区并获取远程图片
            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);
    
            $dirname = './public/uploads/remote/';
            $file['oriName'] = $m ? $m[1] : "";
            $file['filesize'] = strlen($img);
            $file['ext'] = strtolower(strrchr($config['oriName'],'.'));
            $file['name'] = uniqid().$file['ext'];
            $file['fullName'] = $dirname.$file['name'];
            $fullName = $file['fullName'];
    
            //检查文件大小是否超出限制
            if($file['filesize'] >= ($config["maxSize"])){
                $data=array(
                    'state' => '文件大小超出网站限制',
                );
                return json_encode($data);
            }
    
            //创建目录失败
            if(!file_exists($dirname) && !mkdir($dirname,0777,true)){
                $data=array(
                    'state' => '目录创建失败',
                );
                return json_encode($data);
            }else if(!is_writeable($dirname)){
                $data=array(
                    'state' => '目录没有写权限',
                );
                return json_encode($data);
            }
    
            //移动文件
            if(!(file_put_contents($fullName, $img) && file_exists($fullName))){ //移动失败
                $data=array(
                    'state' => '写入文件内容错误',
                );
                return json_encode($data);
            }else{ //移动成功
                $data=array(
                    'state' => 'SUCCESS',
                    'url' => substr($file['fullName'],1),
                    'title' => $file['name'],
                    'original' => $file['oriName'],
                    'type' => $file['ext'],
                    'size' => $file['filesize'],
                );
            }
    
            return json_encode($data);
        }
    
        /*
         * 处理base64编码的图片上传
         * 例如:涂鸦图片上传
        */
        private function upBase64($config,$fieldName){
            $base64Data = $_POST[$fieldName];
            $img = base64_decode($base64Data);
    
            $dirname = './public/uploads/scrawl/';
            $file['filesize'] = strlen($img);
            $file['oriName'] = $config['oriName'];
            $file['ext'] = strtolower(strrchr($config['oriName'],'.'));
            $file['name'] = uniqid().$file['ext'];
            $file['fullName'] = $dirname.$file['name'];
            $fullName = $file['fullName'];
    
            //检查文件大小是否超出限制
            if($file['filesize'] >= ($config["maxSize"])){
                $data=array(
                    'state' => '文件大小超出网站限制',
                );
                return json_encode($data);
            }
    
            //创建目录失败
            if(!file_exists($dirname) && !mkdir($dirname,0777,true)){
                $data=array(
                    'state' => '目录创建失败',
                );
                return json_encode($data);
            }else if(!is_writeable($dirname)){
                $data=array(
                    'state' => '目录没有写权限',
                );
                return json_encode($data);
            }
    
            //移动文件
            if(!(file_put_contents($fullName, $img) && file_exists($fullName))){ //移动失败
                $data=array(
                    'state' => '写入文件内容错误',
                );
            }else{ //移动成功
                $data=array(
                    'state' => 'SUCCESS',
                    'url' => substr($file['fullName'],1),
                    'title' => $file['name'],
                    'original' => $file['oriName'],
                    'type' => $file['ext'],
                    'size' => $file['filesize'],
                );
            }
    
            return json_encode($data);
        }
    
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月13日
  • 已采纳回答 5月5日
  • 修改了问题 4月28日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记