yii2.0 Single File upload and multiple file uploads

Source: Internet
Author: User
Tags yii

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.