A
A
Alexey2018-12-27 14:32:27
Yii
Alexey, 2018-12-27 14:32:27

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']), 
], 
]); 
?>

imageUpload leads to action SaveRedactorImg:
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'); 
} 

}

@images alias:
Yii::setAlias('@images', dirname(dirname(dirname(__DIR__))) . '/public_html/uploads/images');

The essence of the problem is that the ajax request passes, the blog folder is created in upload / images, but the photo is not loaded there (it gives an error An error occurred during the upload process!) What could be the problem? Maybe someone came across.
I'm using the Advanced app

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Orlov, 2018-12-27
@Kefura

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 question

Ask a Question

731 491 924 answers to any question