YII 2.0 File Upload

Source: Internet
Author: User
Tags button type yii

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

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.