Hello,
I’m trying to upload an image file in a form.
MODEL
<?php
namespace app\models;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\web\UploadedFile;
class Model extends ActiveRecord
{
/*
* @return string the name of the file
*/
public $file;
public function rules()
{
return[
//.....
[['file'],'file','skipOnEmpty' => true],
//......
];
}
Controller:
(i’ve included the yii\web\UploadedFile;)
public function actionCreate()
{
$model = new Model();
if($model->load(Yii::$app->request->post())&& $model->validate())
{
// Upload
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/model/'.$model->file->baseName.'.'.$model->file->extension);
//Saving the path......
$model->image = 'uploads/model/'.$model->file->baseName.'.'.$model->file->extension;
//Other Part of the action........
return $this->render('create',[
'model' => $model,
]);
The folders are located inside the view directory
(path /views/uploads/model/)
and they have the 777 permission (working with ubuntu)
and for final the field on the view.
<?= $form->field($model, 'file')->fileInput()->label('Upload an Image') ?>
when i’m trying to save a model record with an image file i get this error:
move_uploaded_file(uploads/images.jpeg): failed to open stream: No such file or directory
where i must collocate the directory for upload a file?
Thanks for all the help