Yii 如何在Yi2 PHP框架上使用CKEDITOR实现文件上传?

Yii 如何在Yi2 PHP框架上使用CKEDITOR实现文件上传?,yii,yii2,ckeditor,Yii,Yii2,Ckeditor,我是Yi2PHP框架的新手。我想在我的文本区域中实现Ckeditor。我得到了库,但尝试使用CKeidtor查找上载文件。我不知道如何在CkEditor中实现文件上传 如果您仍在寻找解决方案或其他人。Migos在Yiii2为Keckeditor建造了一个惊人的平台。要使用图像上载选项实现它,您应该如下所示进行设置: <?= $form->field($model, 'yourParameter') ->widget(CKEditor::className()

我是Yi2PHP框架的新手。我想在我的文本区域中实现Ckeditor。我得到了库,但尝试使用CKeidtor查找上载文件。我不知道如何在CkEditor中实现文件上传

如果您仍在寻找解决方案或其他人。Migos在Yiii2为Keckeditor建造了一个惊人的平台。要使用图像上载选项实现它,您应该如下所示进行设置:

 <?= $form->field($model, 'yourParameter')
         ->widget(CKEditor::className(), 
            [
              'options' => [], 
              'preset' => 'custom',
              'clientOptions' => [
                  'extraPlugins' => '',
                  'height' => 500,

                  //Here you give the action who will handle the image upload 
                  'filebrowserUploadUrl' => '/site/ckeditor_image_upload',

                  'toolbarGroups' => [
                      ['name' => 'undo'],
                      ['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
                      ['name' => 'paragraph', 'groups' => ['list', 'indent', 'blocks', 'align', 'bidi' ]],
                      ['name' => 'styles'],
                      ['name' => 'links', 'groups' => ['links', 'insert']]
                  ]

              ]

            ]) 

?>
 public function beforeAction($action)
{            
    if ($action->id == 'ckeditor_image_upload') {
        $this->enableCsrfValidation = false;
    }

    return parent::beforeAction($action);
}

图像选项现在可以使用了

对于那些像我一样,正在使用CKEditor(基于Francis的解决方案)寻找完整的文件上传指南的人

首先,你应该用它来让你的生活更轻松

在要包含CKEditor的视图中呈现该小部件:

<?= $form->field($model, 'YOUR_FIELD_NAME')->widget(CKEditor::className(), [
    'preset' => 'full',
    'clientOptions' => [
        'filebrowserUploadUrl' => '/CONTROLLER_NAME/upload',
        'filebrowserBrowseUrl' => '/CONTROLLER_NAME/browse'
    ]
]);
?>
如果您的操作将返回除JSON以外的任何内容,它将向您显示带有“未定义”或“错误服务器响应”消息的警报


filebrowserBrowseUrl'=>browse'

如果您不想让用户浏览上传的文件,只需从小部件选项中删除该行即可

在另一种情况下,您应该向控制器添加actionBrowse方法,该方法将呈现页面以浏览和选择图像

控制器方法:

public function actionBrowse(){
    //get function num to pass it to the view (need to be called to pass data of selected file to CKEditor)
    $CKEditorFuncNum = Yii::$app->request->get('CKEditorFuncNum');

    //get list of uploaded files 
    $files = yii\helpers\FileHelper::findFiles($PATH_TO_FOLDER);

    return $this->renderAjax('browse', [
        'funcNum' => $CKEditorFuncNum,
        'files' => $files,
    ]);
}
注意:该视图将作为模式对话框打开,所以我更喜欢使用renderAjax而不是render函数来避免渲染布局。但如果您想使用资产包,我不建议您使用renderPartial

查看文件:

use yii\helpers\Html;

\backend\assets\AppAsset::register($this);

?>
<div class="container">

<h1><?= Html::encode(Yii::t('common', 'Choose any file')) ?></h1>

<?php if (!empty($files)): ?>
    <div class="row">
        <?php foreach ($files as $file):
            $url = "/$file"; ?>
        <div class="col-md-4 mb-2">
            <img 
                src="<?= $url ?>" 
                class="img-thumbnail" 
                style="cursor: pointer; margin-bottom: 2rem"
                onClick="selectImage(<?= $funcNum ?>, '<?= $url ?>')"
            />
        </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
</div>
<script type="text/javascript">
    function selectImage(funcNum, url){
        window.opener.CKEDITOR.tools.callFunction(funcNum, url)
        window.close()
    }
</script>
使用yii\helpers\Html;
\backend\assets\AppAsset::register($this);
?>
" 
class=“img缩略图”
style=“游标:指针;保证金底部:2rem“
onClick=“选择图像(,“”)”
/>
函数selectImage(funcNum,url){
window.opener.CKEDITOR.tools.callFunction(funcNum,url)
window.close()
}

selectImage
函数将调用CKEditor函数,将图像url传递到文件上载模式,然后关闭模式对话框。

您的意思是在编辑器中添加图像?是的。您能帮助我吗?您尝试过使用单击图像图标吗?它要求url,但不在服务器上上载图像。只需显示对话框。我们必须在上面编码t/我不知道该怎么做。试试这个
,它会显示您的上载选项,您需要自己编写上载操作。谢谢您的努力。我今晚会检查它。为了您的努力->向上投票。@NCITCSMOS您能在控制台或网络面板中提供错误消息吗?
public function actionBrowse(){
    //get function num to pass it to the view (need to be called to pass data of selected file to CKEditor)
    $CKEditorFuncNum = Yii::$app->request->get('CKEditorFuncNum');

    //get list of uploaded files 
    $files = yii\helpers\FileHelper::findFiles($PATH_TO_FOLDER);

    return $this->renderAjax('browse', [
        'funcNum' => $CKEditorFuncNum,
        'files' => $files,
    ]);
}
use yii\helpers\Html;

\backend\assets\AppAsset::register($this);

?>
<div class="container">

<h1><?= Html::encode(Yii::t('common', 'Choose any file')) ?></h1>

<?php if (!empty($files)): ?>
    <div class="row">
        <?php foreach ($files as $file):
            $url = "/$file"; ?>
        <div class="col-md-4 mb-2">
            <img 
                src="<?= $url ?>" 
                class="img-thumbnail" 
                style="cursor: pointer; margin-bottom: 2rem"
                onClick="selectImage(<?= $funcNum ?>, '<?= $url ?>')"
            />
        </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
</div>
<script type="text/javascript">
    function selectImage(funcNum, url){
        window.opener.CKEDITOR.tools.callFunction(funcNum, url)
        window.close()
    }
</script>