Yii 2 загрузка изображений

везде пишем вверху use yii\web\UploadedFile;

model

    public function rules()
    {
        return [
            .... 
             //[['img'], 'file', 'skipOnEmpty' => false],
            [['img'], 'file'],
        ];
    }

controler

    public function actionCreate()
    {
        $model = new Unit();

        if ($model->load(Yii::$app->request->post()))
        {
            $file = UploadedFile::getInstance($model, 'img');
            if (isset($file))
            {
                $filename = uniqid() . '.' . $file->extension;
                $path = 'uploads/' . $filename;
                if ($file->saveAs($path))
                {
                    $model->img = $filename;
                }
            }

            if ($model->save())
            {
                return $this->redirect('index');
            }
        }

        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $oldFile = 'uploads/' . $model->img;
        $oldFileName = $model->img;

        if ($model->load(Yii::$app->request->post()))
        {
            //var_dump($oldFileName);
            $file = UploadedFile::getInstance($model, 'img');
            if (isset($file))
            {
               if(file_exists($oldFile)) @unlink($oldFile);
                $filename = uniqid() . '.' . $file->extension;
                $path = 'uploads/' . $filename;
                if ($file->saveAs($path))
                {
                    $model->img = $filename;
                }
            }
            else $model->img = $oldFileName;

            if ($model->save())
            {
                return $this->redirect(['index']);
            }
        }

        return $this->render('update', [
                    'model' => $model,
        ]);
    }

form

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?= $form->field($model, 'img')->fileInput() ?>
    <?php echo ($model->img) ? Html::img('/uploads/' . $model->img, ['width' => 100, 'height' => 100]) : null ?>

index
    GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'class' => 'yii\grid\SerialColumn'
            ],
            //'id',
            [
                'attribute' => 'Изображение',
                'format' => 'html',
                'value' => function($data)
                {
                    return Html::img($data->imageurl, ['width' => '100']);
                },
            ],

view
    DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'attribute' => 'Изображение',
                'value' => $model->imageurl,
                'format' => ['image', ['width' => '100', 'height' => '100']],
            ],



Комментарии

Популярные сообщения из этого блога

Пишем логи на C# (.NET). Легкий способ.

Учебник yii2