yii框架提供了activeFileField控件来完成上传文件(当然也包括了上传图片)的操作,下面介绍yii的activeFileField使用方法。
1、函数原型:
public static string activeFileField(CModel $model, string $attribute, array $htmlOptions=array ( ))
2、调用例子:
(1)首先,设置form,这一步一 定要做,把form设置为’multipart/form-data’,具体请看我的:
<?php $form=$this->beginWidget(‘CActiveForm’, array(‘id’=>’books-form’,‘enableAjaxValidation’=>false,‘htmlOptions’=>array(‘enctype’=>’multipart/form-data’),)); ?>
(2) 接着,在view下的form里设置:
<div class=”row”> <?php echo $form->labelEx($model,’BookImg’); ?> <?php echoCHtml::activeFileField($model,’BookImg’); ?> <?php echo $form->error($model,’BookImg’); ?> </div>
(3) 如果你想预览图片,那么请注意了,可以加上这么一段:
<div class=”row”> <?php echo ‘图片预览’ ?> <?php echo ‘<img src=\'#\'" //www.yerlife.cn/’.$model->BookImg.’” style=”width:200px;height:300px;”/>’; ?> </div>
(4)最后,需要在控制类里加上下面的:
$image=CUploadedFile::getInstance($model,’BookImg’); if (is_object($image) && get_class($image)===’CUploadedFile’) { $model->BookImg=’D:/aaa/aaa.jpg’; //请根据自己的需求生成相应的路径,但是要记得和下面保存路径保持一致 } else { $model->BookImg=’NoPic.jpg’; } if($model->save()) { if (is_object($image) && get_class($image)===’CUploadedFile’) { $image->saveAs(“D:/aaa/aa.jpg”);//路径必须真实存在,并且如果是linux系统,必须有修改权限 } $this->redirect(array(‘view’,’id’=>$model->BookId)); }
请注意:这里是添加的时候使用的,修改的话要有所改变。
(5)限制上传的文件必须是图片,还有限制图片大小,那么请到model层里的rules新增这么一句:
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.’,)
上传图片这块是截取别人的代码,不过,在http://www.yiiframework.com/doc/cookbook/ 里面也有说明。
总结:
上传文件注意5点
1 在views目录下,模块_form.php ,使用‘enctype’=>’multipart/form-data’属性,标示该页面要上传文件,
2 在_form.php文件中,使用CHtml标签activeFileField函数,不过,这里也可以写成html形式,但是,为了代码整洁性,建议使用CHtml的标签函数。
3 设置上传文件属性。这个在model文件里面设置。 array(‘BookImg’, ‘file’,’allowEmpty’=>true,
‘types’=>’jpg, gif, png’,
METHOD | DESCRIPTION | DEFINED BY |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__toString() | String output. | CUploadedFile |
__unset() | Sets a component property to be null. | CComponent |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
getError() | Returns an error code describing the status of this file uploading. | CUploadedFile |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getExtensionName() | CUploadedFile | |
getHasError() | CUploadedFile | |
getInstance() | Returns an instance of the specified uploaded file. | CUploadedFile |
getInstanceByName() | Returns an instance of the specified uploaded file. | CUploadedFile |
getInstances() | Returns all uploaded files for the given model attribute. | CUploadedFile |
getInstancesByName() | Returns an array of instances for the specified array name. | CUploadedFile |
getName() | CUploadedFile | |
getSize() | CUploadedFile | |
getTempName() | CUploadedFile | |
getType() | CUploadedFile | |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
raiseEvent() | Raises an event. | CComponent |
saveAs() | 保存上传图片 |