Yii uses the CForm class (Formbuilder) to upload files

Source: Internet
Author: User
Yii uses the CForm class (Formbuilder) to upload a file model (model)


First, you need a file Upload model:FileUpload. php

1234567891011121314151617 ? Php classFileUploadextendsCFormModel {public $ image;/*** @ return array validation rules for model attributes. */publicfunctionrules () {returnarray (// note you wont need a safe rule here array ('image', 'file', 'allowempty '=> true, 'types' => 'jpg, jpeg, gif, png '),);}}

Note:> for more information about verification rules, see Reference: Model rules validation.

Array used for CForm


Create an array for CForm:UploadForm. php

1234567891011121314151617181920212223242526 'Upload your image', 'bubuckets' => array ('enablesype '=> 'multipart/form-data ',), 'elements' => array ('image' => array ('type' => 'File ',),), 'buttons' => array ('reset' => array ('type' => 'Reset', 'label' => 'Reset ',), 'submit '=> array ('type' => 'Submit', 'label' => 'upload ',),),);
View File)


Create a view file:Upload. php

1234567891011 User-> hasFlash ('success'):?> User-> getFlash ('success');?> Image Upload
Controller)


Add controller and action ):

123456789101112131415161718 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 method here is to perform some operations on the image in your FileUpload model. Note: You do not needFileUpload: imageAssign values, but you can provide a method to access it to achieve a strong model (model) to simplify the controller ).

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.