Answer the question
In order to leave comments, you need to log in
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() ?>
<?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]);
}
}
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
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 questionAsk a Question
731 491 924 answers to any question