Yii-use the CForm class (Formbuilder) to upload files

Source: Internet
Author: User
Although there are many separate file uploads and CForm (formbuilder) documents, there is no combination of the two examples.

Although there are many separate file uploads and CForm (form builder) documents, there is no combination of the two examples.

Model)

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

  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

  'Upload your image', 'attributes' => array( 'enctype' => '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

 user->hasFlash('success')): ?> 
 
 
user->getFlash('success'); ?>

Image Upload

Controller)

Add controller and action ):

 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 ).

This article translated from foreign language website, view the original, please click: http://www.yiiframework.com/wiki/265/file-uploads-in-cform-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.