Home > Backend Development > PHP Tutorial > Yii uses the activeFileField control to implement the method of uploading files and pictures, _PHP tutorial

Yii uses the activeFileField control to implement the method of uploading files and pictures, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:17
Original
942 people have browsed it

yii uses the activeFileField control to implement the method of uploading files and pictures.

This article describes the example of yii using the activeFileField control to implement the method of uploading files and pictures. Share it with everyone for your reference, the details are as follows:

The yii framework provides the activeFileField control to complete the operation of uploading files (including uploading images, of course). The following describes how to use yii's activeFileField.

1. Function prototype:
Copy code The code is as follows: public static string activeFileField(CModel $model, string $attribute, array $htmlOptions=array ( ))
2. Call example:

(1) First, set the form. This step must be done. Set the form to 'multipart/form-data'. For details, please see my:

<&#63;php $form=$this->beginWidget('CActiveForm', array(
'id'=>'books-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
&#63;>

Copy after login

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

<div class="row">
<&#63;php echo $form->labelEx($model,'BookImg'); &#63;>
<&#63;php echo CHtml::activeFileField($model,'BookImg'); &#63;>
<&#63;php echo $form->error($model,'BookImg'); &#63;>
</div>

Copy after login

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

<div class="row">
<&#63;php echo '图片预览' &#63;>
<&#63;php echo '<img src="http://www.XXXX.com/'.$model->BookImg.'" style="width:200px;height:300px;"/>'; &#63;>
</div>

Copy after login

(4) Finally, you need to add the following to 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");//路径必须真实存在,并且如果是linux系统,必须有修改权限
 }
$this->redirect(array('view','id'=>$model->BookId));
}
Copy after login

Please note: This is used when adding. If you modify it, you will need to change it.

(5) It is restricted that the uploaded files must be pictures, and there is also a limit on the size of the pictures, then please go to the rules in the model layer and add this 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.',
)

Copy after login

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Articles you may be interested in:

  • Yii framework login process analysis
  • How to implement batch deletion of CGridView in Yii
  • Yii based on arrays and objects Detailed explanation of model query skills examples
  • Yii permission control methods (three methods)
  • Yii implementation of using CUploadedFile to upload files
  • Creation and creation of Model in Yii Usage method
  • Yii database query method
  • A new method of handling front and backend logins in Yii

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1085887.htmlTechArticleyii uses the activeFileField control to implement the method of uploading files and pictures. This article describes the example of yii using the activeFileField control to implement uploading files and pictures. Picture method. Share it with everyone for everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template