Answer the question
In order to leave comments, you need to log in
How to display images with vova07/yii2-imperavi-widget widget?
Good afternoon. In the vova07/yii2-imperavi-widget widget, I'm trying to implement an image output.
In the form I wrote:
<?php echo $form->field($model, 'text')->widget(Widget::className(), [
'settings' => [
'lang' => 'ru',
'minHeight' => 200,
'plugins' => [
'clips',
'fullscreen',
],
'imageUpload' => \yii\helpers\Url::to(['/site/save-redactor-img', 'sub'=>'blog']),
],
]);
?>
public function actionSaveRedactorImg($sub = 'main')
{
$this->enableCsrfValidation = false;
if (Yii::$app->request->isPost) {
$dir = Yii::getAlias('@images') . '/' . $sub . '/';
if(!file_exists($dir)) {
FileHelper::createDirectory($dir);
}
$result_link = str_replace('admin.', '', Url::home(true)) . 'uploads/images/' . $sub . '/';
$file = UploadedFile::getInstancesByName('file');
$model = new DynamicModel(compact('file'));
$model->addRule('file', 'image')->validate();
if ($model->hasErrors()) {
$result = [
'error' => $model->getFirstError('file')
];
} else {
$model->file->name = strtotime('now') . '_' . Yii::$app->getSecurity()->generateRandomString(6) . '.' . $model->file->extension;
if ($model->file->saveAs($dir . $model->file->name)) {
$result = ['filelink' => $result_link . $model->file->name, 'filename' => $model->file->name];
} else {
$result = [
'error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')
];
}
}
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $result;
} else {
throw new BadRequestHttpException('Only POST is allowed');
}
}
Yii::setAlias('@images', dirname(dirname(dirname(__DIR__))) . '/public_html/uploads/images');
Answer the question
In order to leave comments, you need to log in
Try the elimination method to find where the problem is, ie. comment it out
and see if it works or not.
If the problem is here, then check if everything is OK with the path here
. If not, go to the SaveAs code and look already there.
The text of the error says that there is a problem somewhere, but where it is not clear. You have to debug.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question