D
D
des1roer2015-07-02 06:23:57
Yii
des1roer, 2015-07-02 06:23:57

How to upload an image?

I'm trying to upload an image based on yii 2 basic.
followed the instructions path
https://github.com/yiisoft/yii2/blob/master/docs/g...
localhost\gameproj\models\UploadForm.php

<?php
namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

/**
 * UploadForm is the model behind the upload form.
 */
class UploadForm extends Model
{
    /**
     * @var UploadedFile file attribute
     */
    public $file;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            ) ?>

<?= $form->field($model, 'file')->fileInput() ?>

<button>Отправить</button>

<?php ActiveForm::end() ?>

localhost\gameproj\controllers\UploadController.php
<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;

class SiteController extends Controller
{
    public function actionUpload()
    {
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');

            if ($model->file && $model->validate()) {                
                $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
            }
        }

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

gameproj/upload
get
Unknown Class - yii\base\UnknownClassException
Unable to find 'app\controllers\UploadController' in file: D:\open\OpenServer\domains\localhost\gameproj/controllers/UploadController.php. Namespace missing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
des1roer, 2015-07-02
@des1roer

really looked through

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;

class UploadController extends Controller
{
    public function actionIndex()
    {
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');

            if ($model->file && $model->validate()) {                
                $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
            }
        }

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question