phpyii2ckeditorkcfinder

Yii2 KCFinder : How to upload images to "Common" or "Frontend" directory


I run into a problem. I use CKEditor to crete HTML Editor and also use KCFinder to upload&insert images within the HTML editor. My problem is, I cannot show the images,that i uploaded via KCFinder, in my FrontEnd website

My code (In backend/view/_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>

Now my code can work correctly but the images will be uploaded to "backend/web/upload" How can i upload the images via KCFinder to "frontend/web/upload" ? Or Is there any suggestion solution for the case? I need to use CKEditor+KCFinder to create a news form then I can show the content in Frontend Website. Thank you very much for your help.


Solution

  • Finally, I found the solution. I created the link by using command :

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

    After that, I edited the httpd-vhosts.conf to allow "Options +FollowSymlinks".

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

    Do not forget to "restart" apache service. :)

    Reference site -> http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/