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 $htmlOpti ())
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. ' " /> ';?>
(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.
The above describes the use of Yii Activefilefield control to upload files and pictures of the method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.