【问题标题】:Yii2 KCFinder : How to upload images to "Common" or "Frontend" directoryYii2 KCFinder:如何将图片上传到“Common”或“Frontend”目录
【发布时间】:2015-08-25 11:36:32
【问题描述】:

我遇到了问题。我使用 CKEditor 来创建 HTML 编辑器,还使用 ​​KCFinder 在 HTML 编辑器中上传和插入图像。我的问题是,我无法在我的前端网站中显示通过 KCFinder 上传的图像

我的代码(在后端/视图/_form 中)

 <?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\modules\CKEditor;
use iutbay\yii2kcfinder\KCFinder;

$kcfOptions = array_merge(KCFinder::$kcfDefaultOptions, [
    //'uploadURL' => Yii::getAlias('@web').'/upload',
    'uploadURL' => Yii::getAlias('@common').'/upload',
    'access' => [
        'files' => [
            'upload' => true,
            'delete' => true,
            'copy' => true,
            'move' => true,
            'rename' => true,
        ],
        'dirs' => [
            'create' => true,
            'delete' => true,
            'rename' => true,
        ],
    ],
]);

// Set kcfinder session options
Yii::$app->session->set('KCFINDER', $kcfOptions);
?>

<div class="emails-form">
    <?php yii\widgets\Pjax::begin(['id' => 'new_email']) ?>
    <?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data' ]]); ?>
    <?= $form->field($model, 'receiver_name')->textInput(['maxlength' => 200]) ?>
    <?= $form->field($model, 'receiver_email')->textInput(['maxlength' => 200]) ?>
    <?= $form->field($model, 'subject')->textInput(['maxlength' => 200]) ?>
    <?//= $form->field($model, 'content')->textarea(['maxlength' => 200]) ?>
    <?= $form->field($model, 'content')->widget(CKEditor::className(), [
        'options' => ['rows' => 6],
        'preset' => 'full'
        //'preset' => 'basic'
    ])
    ?>
    <?= $form->field($model, 'attachment')->fileInput(['maxlength' => 200]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
    <?php yii\widgets\Pjax::end() ?>
</div>

现在我的代码可以正常工作,但图像将上传到“后端/网络/上传”我如何通过 KCFinder 将图像上传到“前端/网络/上传”? 或者有没有针对此案的建议解决方案?我需要使用 CKEditor+KCFinder 创建一个新闻表单,然后我才能在前端网站中显示内容。 非常感谢您的帮助。

【问题讨论】:

    标签: ckeditor yii2 kcfinder


    【解决方案1】:

    看看 Yii2 提供了前端/后端供我们使用。我建议暂时忘记它在后端。
    您可以做的是设置参数,它将转到后端/web/updloads 并仅将文件名保存在数据库中,我的意思是上传文件夹后的文件名/路径。 像这样:

    这将是您的 bakcend:backend.example.com/uploads/ 上传后数据库中的文件名:file.jpg、profile/firstuser.png。

    当您能够从前端访问它时,只需将您的上传文件夹从后端复制到前端即可。

    我知道这不是写解决方案,但它对我有用。我想它会帮助我。

    这是我的第一个答案,如果我不清楚,请询问:)

    谢谢

    【讨论】:

    • 您好,谢谢您的回答,但我无法使用该解决方案。因为我的目标是创建一个新闻模块,允许后端用户创建新闻,然后新闻将显示在前端(公共用户可以阅读新闻)。所以我需要找到一个解决方案,允许通过使用 CKFinder 将文件从后端上传到前端/web/upload。
    【解决方案2】:

    最后,我找到了解决方案。我使用命令创建了链接:

    cd /path/to/project/frontend/web
    ln -s ../../backend/web/upload upload
    

    在那之后,我编辑了 httpd-vhosts.conf 以允许 "Options +FollowSymlinks"

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot "/path/to/project/frontend/web"
        Options +FollowSymlinks
        ...
    </VirtualHost>
    

    不要忘记“重启”apache 服务。 :)

    参考网站 -> http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2016-08-04
      • 2014-07-10
      • 1970-01-01
      • 2023-03-17
      • 2017-03-10
      • 1970-01-01
      相关资源
      最近更新 更多