Yii uses Activefilefield controls to implement methods for uploading files and pictures, _php tutorial

Source: Internet
Author: User

Yii uses the Activefilefield control to implement the method of uploading files and pictures,


The example in this paper describes how Yii uses the Activefilefield control to upload files and pictures. Share to everyone for your reference, as follows:

The YII framework provides the Activefilefield control to complete the operation of uploading a file (including uploading a picture, of course), which describes the Activefilefield usage of yii.

1. Function prototype:
Copy the Code Code as follows: public static string Activefilefield (CModel $model, string $attribute, array $htmlOptions =array ())
2. Invocation Example:

(1) First, set the form, this step must be done, the form is set to ' Multipart/form-data ', specifically please see my:

<?php $form = $this->beginwidget (' Cactiveform ', array (' id ' = ' books-form ', ' enableajaxvalidation ' =>false , ' Htmloptions ' =>array (' enctype ' = ' multipart/form-data '),);? >

(2) Next, in the form under View, set:

<?php echo $form->labelex ($model, ' bookimg ');? ><?php Echo Chtml::activefilefield ($model, ' bookimg ');? ><?php echo $form->error ($model, ' bookimg ');?>

(3) If you want to preview the picture, then please note that you can add this paragraph:

<?php echo ' picture preview '? ><?php echo ' bookimg. ' "style=" width:200px;height:300px; " /> ';?>

(4) Finally, you need to add the following in the control class:

if ($model->save ()) {$image =cuploadedfile::getinstance ($model, ' bookimg '); if (Is_object ($image) && get_ Class ($image) = = = ' Cuploadedfile ') {$image->saveas ("d:/aaa/aa.jpg");//The path must be real, and if it is a Linux system, you must have Modify permissions} $this Redirect (Array (' View ', ' id ' = = $model->bookid));}

Please note: This is used when adding, change the words to be changed.

(5) Limit the uploaded files must be pictures, as well as limit the size of the picture, then go to the model layer of rules added such a sentence:

Array (' bookimg ', ' file ', ' AllowEmpty ' =>true, ' types ' = ' jpg, gif, png ', ' maxSize ' =>1024 * 1024 * 1,// 1MB ' toolarge ' = ' The file was larger than 1MB. Please upload a smaller file. ', ')

It is hoped that this article is helpful to the PHP program design based on YII framework.

Articles you may be interested in:

    • Analysis of YII Framework login process
    • The method of Cgridview implementation in Yii for batch deletion
    • Yii examples of model query techniques based on arrays and objects
    • Methods of Yii Authority control (three methods)
    • Yii implementation method of uploading files using Cuploadedfile
    • Modeling (model) creation and its use in Yii
    • The query method of Yii database
    • A new method to implement pre-and post-logon in Yii

http://www.bkjia.com/PHPjc/1085887.html www.bkjia.com true http://www.bkjia.com/PHPjc/1085887.html techarticle Yii uses the Activefilefield control to implement the method of uploading files and pictures, the example of this article describes the way Yii uses Activefilefield control to upload files and pictures. Share to everyone ...

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