Yii2 file upload used to yii2 file upload class UploadFile, as well as the corresponding model rules, here are the single file upload and multiple file upload:
Yii2 Single File Upload:
To upload a step, first create the upload form model (including validation rules), followed by the Controller operation Action, and the corresponding view:
Model layer:
upload.php [Single File upload model]
<?php
namespace App\models;
Use Yii;
Use Yii\base\model;
class Upload extends model{
Public $file;
Public function Rules () {
return [
[[' File '], ' file ', ' extensions ' = ' jpg, png ', ' mimetypes ' = ' image/jpeg, Image/png ',],
];
}
Public function Attributelabels () {
return [
' file ' = ' files upload '
];
}
}
uploadform.php [multi-file upload model]
<?php
namespace App\models;
Use Yii;
Use Yii\base\model;
class Uploadform extends Model
{
/**
* @var uploadedfile| Null file attribute
*/
Public $file;
/**
* @return Array the validation rules.
*/
Public function rules ()
{
return [
[[' File '], ' file ', ' maxfiles ', ' extensions ' = ' jpg,png,gif '],
];
}
Public function Attributelabels () {
return [
' file ' + ' Multiple files upload '
];
}
Controller layer, upload operation and upmore operation in TestController
<?php
namespace App\controllers;
Use Yii;
Use Yii\web\controller;
Use app\models\upload;
Use app\models\uploadform;
Use yii\web\uploadedfile;
class TestController extends controller{
Public function Actionindex () {
return $this->renderpartial (' index ');
}
/**
* @return String|\yii\web\response
* Single File Upload
*/
Public function Actionupload () {
$model = new Upload ();
if (Yii:: $app->request->ispost) {
$file = uploadedfile::getinstance ($model, ' file ');
$path = ' uploads/'. Date ("Ymdh", Time ()). ' /';
if ($file && $model->validate ()) {
if (!file_exists ($path)) {
mkdir ($path, 0775,true);
}
$file->saveas ($path. Time (). ‘.‘ . $file->getextension ());
Yii:: $app->session->setflash (' success ', ' upload success! ');
return $this->redirect (' upload ');
}
}
return $this->render (' Upload ', [' model ' = ' $model]);
}
Public function Actionupmore () {
$model = new Uploadform ();
if (Yii:: $app->request->ispost) {
$file = uploadedfile::getinstances ($model, ' file ');
if ($file && $model->validate ()) {
echo "<pre/>";
foreach ($file as $FL) {
$fl->saveas (' uploads/'. Mt_rand (1100,9900). Time (). $FL->basename. ‘.‘ . $FL->extension);
}
Yii:: $app->session->setflash (' success ', ' upload success! ');
return $this->redirect (' Upmore ');
}
}
return $this->render (' Upmore ', [' model ' = ' $model]);
}
}
View: Layer
[Single File View uplod.php]
<?php
Use yii\helpers\html;
Use yii\widgets\activeform;
?>
<!doctype html>
<meta charset= "UTF-8" >
<title>Document</title>
<body>
<?php if (Yii:: $app->session->hasflash (' success ')):?>
<div class= "alert Alert-danger" >
<?=yii:: $app->session->getflash (' success ')?>
</div>
<?php endif?>
<?php $form =activeform::begin ([
' id ' = ' upload ',
' enableajaxvalidation ' = False,
' Options ' =>[' enctype ' = ' multipart/form-data '
]);
?>
<?= $form->field ($model, ' file ')->fileinput ();? >
<?= Html::submitbutton (' Submit ', [' class ' = ' btn ' btn-primary ', ' name ' = ' Submit-button '])?>
<?php activeform::end ();?>
</body>
[Multi-file View upmore.php]
<?php
Use yii\helpers\html;
Use yii\widgets\activeform;
?>
<!doctype html>
<meta charset= "UTF-8" >
<title>Document</title>
<body>
<?php if (Yii:: $app->session->hasflash (' success ')):?>
<div class= "alert Alert-danger" >
<?=yii:: $app->session->getflash (' success ')?>
</div>
<?php endif?>
<?php $form =activeform::begin ([
' id ' = ' upload ',
' enableajaxvalidation ' = False,
' Options ' =>[' enctype ' = ' multipart/form-data '
]);
?>
<?= $form->field ($model, ' file[] ')->fileinput ([' multiple ' = true]);? >
<?= Html::submitbutton (' Submit ', [' class ' = ' btn ' btn-primary ', ' name ' = ' Submit-button '])?>
<?php activeform::end ();?>
</body>
yii2.0 Single File upload and multiple file uploads