yii2文件上传

1.在companies表里面添加一个名字为logo类型varchar(255)字段

 

2.修改models下的companies.php文件

class Companies extends \yii\db\ActiveRecord
{

   
 public $file;

 

     

/**
*
 @inheritdoc
*/

public function rules()
{
   
 return [
        [[
'company_name' , 'company_email', 'company_address', 'company_created_date' ,'company_created_date', 'company_status' ], 'required'],
        [[
'company_created_date' ], 'safe'],
        [[
'company_status' ], 'string'],
        [[
'file' ], 'file'],
        [[
'company_name' , 'logo', 'company_email', 'company_address' ], 'string', 'max' => 255]
    ];
}

 

/**
*
 @inheritdoc
*/

public function attributeLabels()
{
   
 return [
       
 'company_id' => 'Company ID',
       
 'company_name' => 'Company Name',
       
 'company_email' => 'Company Email',
       
 'company_address' => 'Company Address',
       
 'file' => 'Logo',
       
 'company_created_date' => 'Company Created Date',
       
 'company_status' => 'Company Status',
    ];
}

}

 

3.在web文件夹加下面新建一个名字是uploads的文件夹

 

 

4.修改_form.php

<?php

use yii\helpers \Html;
use yii\ widgets\ActiveForm;

/* @var $this yii\web\View */
/*
 @var $model backend\models\Companies */
/*
 @var $form yii\widgets\
ActiveForm */
?>

<div class= "companies-form">

   
 <?php $form = ActiveForm::begin (['options'=> ['enctype'=> 'multipart/form-data']]); ?>

     
#code ... 

    <?= $form ->field( $model, 'file' )->fileInput () ?>


    <?=
 $form ->field( $model, 'company_status' )->dropDownList ([ 'active' => 'Active' , 'inactive' => 'Inactive', ], ['prompt' => 'Status']) ?>

   
 < div class="form-group" >
       
 <?= Html ::submitButton( $model->isNewRecord ? 'Create' : 'Update', ['class' => $model-> isNewRecord ? 'btn btn-success' : 'btn btn-primary' ]) ?>
   
 </ div>

   
 <?php ActiveForm::end (); ?>

</div>

 

5.修改控制器CompaniesController.php

/**
* Creates a new Companies model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/

public function actionCreate()
{
   
 if (Yii:: $app->user ->can( 'create-company')){
       
 $model = new Companies ();

       
 if ($model-> load(Yii ::$app-> request->post ()) ) {

           
 $imageName =$model-> company_name;
           
 // get the instance of  the uploaded file
           
 $model->file =UploadedFile:: getInstance($model ,'file');

           
 $model->file ->saveAs( 'uploads/'.$imageName .'.'. $model->file ->extension);

           
 // save the path in the db column
           
 $model->logo ='uploads/'. $imageName.'.' .$model-> file->extension ;

           
 $model ->company_start_date=date ('Y-m-d H:i:s');

           
 $model ->company_created_date=date ('Y-m-d H:i:s');

           
 $model ->save();

           
 return $this ->redirect([ 'view', 'id' => $model->company_id ]);
        }
 else {
           
 return $this ->render( 'create', [
               
 'model' => $model,
            ]);
        }
    }
else {
       
 throw new ForbiddenHttpException ;
    }

}

 

 

来自于datou:https://github.com/datou-leo/ci

posted @ 2015-06-09 11:19  开源  阅读(255)  评论(0)    收藏  举报