Answer the question
In order to leave comments, you need to log in
Loading a picture?
Hello, I have a banal problem, I can’t upload a picture, as I understand it, the file was lost ??
I use use kartik\widgets\FileInput
move_uploaded_file(upload/company/5e731b12f8569cc09ba8237f54ec4bb0.jpg): failed to open stream: No such file or directory
<?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(); ?>
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,
]);
}
}
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);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question