Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问作者此项目能不能和ueditor配合使用直接对接到WEB后台 #49

Open
sw586 opened this issue Jul 8, 2020 · 25 comments

Comments

@sw586
Copy link

sw586 commented Jul 8, 2020

ueditor通过PicUploader上传图片到github

@sw586 sw586 changed the title 请问制作此项目能不能和ueditor配合使用直接对接到WEB后台 请问作者此项目能不能和ueditor配合使用直接对接到WEB后台 Jul 8, 2020
@xiebruce
Copy link
Owner

xiebruce commented Jul 8, 2020

可以的,但需要修改ueditor的上传代码,我下载个ueditor测试一下吧,然后更新一下文档你再看就行。

@sw586
Copy link
Author

sw586 commented Jul 9, 2020

太好了 真想亲你一口

@sw586
Copy link
Author

sw586 commented Jul 9, 2020

或者应该怎么联系上您,如果在自己搞不定情况下 能不能付费请您处理,我的小站是用的ueditor,很想结合使用本项目

@xiebruce
Copy link
Owner

xiebruce commented Jul 9, 2020

@sw586 你先试试能不能解决,如果需要我帮忙直接在这里回复就行,解决后给你个打赏码。

@sw586
Copy link
Author

sw586 commented Jul 9, 2020

好的,您能大概写思路么?就是假如本地有个ueditor和PicUploader两个站点,应该怎么来调用PicUploader。实现在ueditor上传图片到github

@xiebruce
Copy link
Owner

xiebruce commented Jul 9, 2020

ueditor和picuploader在同一台机器上时,可通过命令来调用PicUploader上传:

/path/to/php /path/to/PicUploader/index.php --type=typora /path/to/test.jpg

你需要在ueditor的php上传方法里修改一下,用php的exec()去执行上边的命令就行,它会返回一个上传后的链接(纯链接,非markdown格式)。


当ueditor与PicUploader不在同一台机器上时,就需要使用curl来post上传这个文件,PicUploader那边就是常规的php接收文件方式,即用$_FILES来接收上传文件的,你需要组装curl上传参数,让PicUploader能接收到这个$_FILES['ueditor']这个变量的值就行了(注意无需修改PicUploader,要修改的是ueditor的这个文件ueditor-utf8-php/php/Uploader.class.php)。

@sw586
Copy link
Author

sw586 commented Jul 9, 2020

遗憾,,没搞定,能搞个demo么

@sw586
Copy link
Author

sw586 commented Jul 9, 2020

ueditor和picuploader在同一台机器上时,需要在同一个站点下吗

@xiebruce
Copy link
Owner

xiebruce commented Jul 10, 2020

不需要在同一站点下的。

uploader.class.php中找到这个方法private function upFile(),在这个方法最后有一个if else,注释掉else里原来的那句,添加一句调用自定义方法$this->uploadByPicUploader();

//移动文件
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];
    // 添加
    $this->uploadByPicUploader();
}

这是自定义方法,你粘贴到uploader.class.php文件里(可以放在upFile()方法后面),注意要把$phpPath$picUploaderPath两个变量的值换成你服务器里的(用绝对路径)

/**
 * 自定义上传函数(调用PicUploader上传)
 */
private function uploadByPicUploader($type='local'){
    if($type == 'local'){
        $phpPath = '/usr/local/opt/[email protected]/bin/php';
        $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php';
        $option = '--type=typora';
        $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $this->filePath;
        exec($cmd, $output);
        if(isset($output[0])){
            $this->fullName = $output[0];
            $this->stateInfo = $this->stateMap[0];
        }
    }else{
        //处理ueditor与PicUploader不在同一台机器上的问题
    }
}

另外要注意php.ini中的disable_functions不要有exec,否则这函数会被被禁用,也就无法执行。

@sw586
Copy link
Author

sw586 commented Jul 11, 2020

太牛了 居然成功了!!

@sw586
Copy link
Author

sw586 commented Jul 11, 2020

大佬帮看一下下面这种上传方法应该怎么来修改应用:

    /**
     * 文件上传
     */
    public function upload() {

        // 验证上传权限
        $this->_check_upload_auth();
        $p = $this->_get_upload_params();
        $rt = \Phpcmf\Service::L('upload')->upload_file([
            'path' => '',
            'form_name' => 'file_data',
            'file_exts' => @explode(',', $p['exts']),
            'file_size' => (int)$p['size'] * 1024 * 1024,
            'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info((int)$p['attachment'], (int)$p['image_reduce']),
        ]);
        if (!$rt['code']) {
            exit(dr_array2string($rt));
        }

        // 附件归档
        $data = \Phpcmf\Service::M('Attachment')->save_data($rt['data']);
        if (!$data['code']) {
            exit(dr_array2string($data));
        }

        // 上传成功
        if (IS_API_HTTP) {
            $data['data'] = [
                'id' => $data['code'],
                'url' => $rt['data']['url'],
            ];
            exit(dr_array2string($data));
        } else {
            exit(dr_array2string(['code' => 1, 'msg' => dr_lang('上传成功'), 'id' => $data['code'], 'info' => $rt['data']]));
        }

    }

完整文件:[https://github.com/sw586/uploadfile/blob/master/File.php]
@xiebruce

@xiebruce
Copy link
Owner

现在才看到,你这代码发上来全乱了,你直接把你二次开发过的ueditor压缩包发我邮箱吧:[email protected],另外,感谢vx打赏!

@sw586
Copy link
Author

sw586 commented Jul 11, 2020

感谢大佬!已经打包发到您邮箱,最近不景气。。打赏莫嫌少就好!!我会再次打赏您的,不会白占用您的时间,感谢您这么热心的回复及帮助!

@xiebruce
Copy link
Owner

xiebruce commented Jul 11, 2020

研究了一下,这个比之前直接用的原版ueditor麻烦,主要是研究过程麻烦,我得安装你的cms,又得研究二次开发后的ueditor上传图片逻辑。

private function upFile()方法的最后几行注释掉并添加一行调用自定义函数

// $this->fileUrl = $this->attachment_info['url'].$this->fullName;
// $this->stateInfo = $this->stateMap[0];

// 存储附件
// $this->save_attach($rt);
//添加这行(调用自定义函数)
$this->uploadByPicUploader($rt);

这是自定义函数(注意要把$phpPath和$picUploaderPath两个变量的值换成你服务器里的(用绝对路径))

/**
 * 自定义上传函数(调用PicUploader上传)
 */
private function uploadByPicUploader($rt, $type='local'){
    $filePath = ROOTPATH . 'uploadfile/' . $this->fullName;
    if($type == 'local'){
        $phpPath = '/usr/local/opt/[email protected]/bin/php';
        $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php';
        $option = '--type=typora';
        $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $filePath;
        exec($cmd, $output);
        if(isset($output[0])){
            $this->fileUrl = $output[0];
            $this->stateInfo = $this->stateMap[0];
            // 存储附件
            $this->save_attach($rt);
        }
    }else{
        //处理ueditor与PicUploader不在同一台机器上的问题
    }
}

另外要注意php.ini中的disable_functions不要有exec,否则这函数会被被禁用,也就无法执行。

@sw586
Copy link
Author

sw586 commented Jul 12, 2020

多谢搞定了

@sw586
Copy link
Author

sw586 commented Jul 12, 2020 via email

@xiebruce
Copy link
Owner

不客气,也感谢再次打赏!

@xiebruce
Copy link
Owner

改造?你要改造以实现什么功能吗?

@helenpayne
Copy link

就是把上面的public function upload() 也能类似ueditor那么调用PicUploader上传,另外问一下PicUploader有办法上传文件夹么

@xiebruce
Copy link
Owner

这个upload()的功能体现在页面哪里(也就是页面哪个地方上传文件是调用upload的)?

上传文件夹理论上是可以的,不过有点麻烦,我看什么时候研究一下。

@helenpayne
Copy link

是这种一般的单文件上传按钮,就在发给您的包里
TIM截图20200712171829

@sw586
Copy link
Author

sw586 commented Jul 12, 2020

是这种一般的单文件上传按钮,就在发给您的包里
TIM截图20200712171829

大佬撤回上面的咨询, 我自己搞定了,感谢你耐心解答!!祝大佬生活愉快 早日实现人生巅峰!!!

@xiebruce
Copy link
Owner

dayrui/Core/Controllers/Api/File.php文件中找到upload()函数,在// 附件归档前面添加一行调用自定义函数的代码

//添加这行(调用自定义函数)
$rt = $this->uploadByPicUploader($rt);

// 附件归档
$data = \Phpcmf\Service::M('Attachment')->save_data($rt['data']);
if (!$data['code']) {
    exit(dr_array2string($data));
}

这是自定义函数,把它放到dayrui/Core/Controllers/Api/File.php文件中

/**
 * 自定义上传函数(调用PicUploader上传)
 * @param        $rt
 * @param string $type
 *
 * @return mixed|string
 */
public function uploadByPicUploader($rt, $type='local'){
    $fileUrl = '';
    $filePath = $rt['data']['path'];
    if($type == 'local'){
        $phpPath = '/usr/local/opt/[email protected]/bin/php';
        $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php';
        $option = '--type=typora';
        $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $filePath;
        exec($cmd, $output);
        if(isset($output[0])){
            $fileUrl = $output[0];
        }
    }else{
        //处理ueditor与PicUploader不在同一台机器上的问题
    }
    if(preg_match('/http[s]?:\/\/(.*?)$/', $fileUrl)){
        $rt['data']['url'] = $fileUrl;
        $rt['data']['preview'] = '<a href="javascript:dr_preview_image(\'' . $fileUrl . '\');"><img src="' . $fileUrl . '"></a>';
    }
    return $rt;
}

@helenpayne
Copy link

太好了!谢谢

@xiebruce
Copy link
Owner

不客气

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants