Create a (uploadform.php) model layer First
<?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 ()
{
/' skiponempty ' + false verify
//' Extensions ' = = ' png, JPG, GIF, jpeg ' uploaded file format
return [
[[' File '], ' file '],
];
}
}
------------------------------------------------------------------------------
Controller layer (uploadcontroller.php)
<?php
namespace Backend\controllers;
Use Yii;
Use Yii\web\controller;
Use App\models\uploadform;
Use Yii\web\uploadedfile;
Class Uploadcontroller extends controller{
function Actionindex () {
$request = Yii:: $app->request;
$model = new Uploadform ();
if ($request->ispost)
{
$model->file = uploadedfile::getinstance ($model, ' file ');
if ($model->file && $model->validate ())
{
$r = $model->file->saveas ('./upload/'. $model->file->basename. ‘.‘. $model->file->extension); This is the upload, upload folder to be manually created yourself
Get the name of the uploaded file
$r = $model->file;
$image = $r->name;
Add Inbound
$upload = \yii:: $app->db;
$query = $upload->createcommand ()->insert ("Upload", [' image ' = + $image]);
if ($query->execute ())
{
$this->redirect (Array (' Upload/index '));
}
}
}
Else
{
return $this->render (' Upload ', [' model ' = ' $model]);
}
}
}
-----------------------------------------------------------------------------------------------------
View Layer (upload.php)
<?php
Use Yii\widgets\activeform;
Use Yii\helpers\url;
?>
<?php $form = Activeform::begin ([' options '] = [' enctype ' = ' multipart/form-data ']]?>
<table class= "Table table-bordered table-hover definewidth M10" >
<tr>
<TD class= "Tableleft" > Upload image </td>
<td><?= $form->field ($model, ' file ')->fileinput ()?></td>
</tr>
<tr>
<TD class= "Tableleft" ></td>
<td>
<button type= "Submit" class= "btn btn-primary" type= "button" > Save </button>
</td>
</tr>
<?php activeform::end ()?>
---restore content ends---
---restore content starts---
Create a (uploadform.php) model layer First
<?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 ()
{
' Skiponempty ' = False verification
' Extensions ' = ' png, JPG, GIF, JPEG ' format of uploaded files
return [
[[' File '], ' file '],
];
}
}
------------------------------------------------------------------------------
Controller layer (uploadcontroller.php)
<?php
namespace Backend\controllers;
Use Yii;
Use Yii\web\controller;
Use App\models\uploadform;
Use Yii\web\uploadedfile;
Class Uploadcontroller extends controller{
function Actionindex () {
$request = Yii:: $app->request;
$model = new Uploadform ();
if ($request->ispost)
{
$model->file = uploadedfile::getinstance ($model, ' file ');
if ($model->file && $model->validate ())
{
$r = $model->file->saveas ('./upload/'. $model->file->basename. ‘.‘. $model->file->extension); This is the upload, upload folder to be manually created yourself
Get the name of the uploaded file
$r = $model->file;
$image = $r->name;
Add Inbound
$upload = \yii:: $app->db;
$query = $upload->createcommand ()->insert ("Upload", [' image ' = + $image]);
if ($query->execute ())
{
$this->redirect (Array (' Upload/index '));
}
}
}
Else
{
return $this->render (' Upload ', [' model ' = ' $model]);
}
}
}
-----------------------------------------------------------------------------------------------------
View Layer (upload.php)
<?php
Use Yii\widgets\activeform;
Use Yii\helpers\url;
?>
<?php $form = Activeform::begin ([' options '] = [' enctype ' = ' multipart/form-data ']]?>
<table class= "Table table-bordered table-hover definewidth M10" >
<tr>
<TD class= "Tableleft" > Upload image </td>
<td><?= $form->field ($model, ' file ')->fileinput ()?></td>
</tr>
<tr>
<TD class= "Tableleft" ></td>
<td>
<button type= "Submit" class= "btn btn-primary" type= "button" > Save </button>
</td>
</tr>
<?php activeform::end ()?>
YII 2.0 File Upload