Yii uploads files using the Cform class (Form builder)

Source: Internet
Author: User
Tags yii

Models (model)

First you need a file upload model:fileupload.php

<?phpClassFileUploadextends cformmodel {public  $image; /** *  @return array validation rules for model attributes. */public function rules () {return array (//note you wont need a safe rule here array (  ' image ',  ' file ',  ' allowempty ' = true,  ' types ' =  ' jpg, JPEG, GIF, PNG '),); } } 

Note:> For more validation rules, see Reference:model Rules validation

Array for CForm

The following creates an array for CForm: uploadform.php

<?phpReturnArray' Title ' =' Upload your image ',' Attributes ' =Array' Enctype ' = ' elements ' = array ( ' image ' = array (  ' type ' =  ' file ', "),  ' buttons ' = > array ( ' reset ' = array ( Span class= "hljs-string" > ' type ' =  ' reset ',  ' label ' = Span class= "hljs-string" > ' Reset ', " ' submit ' = array ( Span class= "hljs-string" > ' type ' =  ' submit ',  ' label ' = Span class= "hljs-string", "Upload",),),            

Views file (View)

Create view file: upload.php

<?phpif (Yii::app ()->user->hasflash (' Success ')):?> <divclass= "info" > <?php echo yii::app ()->< Span class= "Hljs-title" >user->getflash (' success ');?> </div><? PHP ENDIF;?> <h1>Image upload</h1> <div class= "form" ><? PHP echo $form;? ></div>  

Controllers (Controller)

Add Controller and Action (action):

<?phpClassFileuploadcontrollerExtendsCcontroller {PublicfunctionActionupload() {$model =New FileUpload ();$form =New CForm (' Application.views.fileUpload.uploadForm ',$model);if ($form->submitted (' Submit ') && $form->validate ()) { $form->model->image = cuploadedfile::getinstance ( $form->model,  ' image '); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //do something with your image here //!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!! Yii::app ()->user->setflash ( ' success ',  ' File uploaded ') ;  $this->redirect (array ( Upload ')); }  $this->render ( ' upload ',  Array ( ' form ' +  $form));}      

The best way to do this here is to perform some action on the picture in your FileUpload model. Note: You do not need to assign a value to fileupload::image , but you can provide a way to access it to achieve the robust model simplification controller.

Reproduced from HDR

Yii uploads files using the Cform class (Form builder)

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.