Привет, у меня банальная проблема не могу загрузить картинку, как я понял потерялся файл??
изпользую use kartik\widgets\FileInput
move_uploaded_file(upload/company/5e731b12f8569cc09ba8237f54ec4bb0.jpg): failed to open stream: No such file or directory
form
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'image')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
'pluginOptions' => [
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'browseClass' => 'btn btn-primary btn-block',
'browseIcon' => '<i class="glyphicon glyphicon-picture"></i> ',
'browseLabel' => 'Выбрать фото'
],
]); ?>
<?php ActiveForm::end(); ?>
controller
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post())) {
if ($model->saveImage()) {
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
model
public $image;
[['image'], 'file', 'extensions' => 'png, jpg', 'maxFiles' => 1, 'maxSize' => 1024 * 1024 * 1],
public function saveImage()
{
if ($this->image = UploadedFile::getInstance($this, 'image')) {
if (!empty($this->image) && $this->image->size !== 0) {
$objectFile = new ObjectFile();
$objectFile->name = md5(microtime()) . '.' . $this->image->extension;
$objectFile->item_id = $this->id;
$objectFile->class = $this->formName();
if ($objectFile->save(false)) {
$this->image->saveAs(Yii::getAlias('uploads/product') . DIRECTORY_SEPARATOR . $objectFile->name);
}
}
}
}