yii上传图片、yii上传文件、yii控件activeFileField使用

 

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 echo CHtml::activeFileField($model,’BookImg’); ?>
<?php echo $form->error($model,’BookImg’); ?>
</div>
(3) 如果你想预览图片,那么请注意了,可以加上这么一段:
<div class=”row”>
<?php echo ‘图片预览’ ?>
<?php echo ‘<img src=”http://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.’,
)

<?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 echo CHtml::activeFileField($model,’BookImg’); ?> <?php echo $form->error($model,’BookImg’); ?> </div>
(3) 如果你想预览图片,那么请注意了,可以加上这么一段:
<div class=”row”> <?php echo ‘图片预览’ ?> <?php echo ‘<img src=”http://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’,

‘maxSize’=>1024 * 1024 * 1, // 1MB
‘tooLarge’=>’文件最大不超过1MB,请重新上传文件’,
)
4 在controllers文件中,将上传文件名写入数据库以及保存图片到服务器。
$model=new yii_product_list;
$model->attributes=$_POST['yii_product_list'];    //将页面提交值赋值给yii_product_list’对象
$product_imgage = CUploadedFile::getInstance($model,’product_imgage’); 接收上传图片名
$model->product_imgage = $product_imgage; 将图片名,重新赋给product_imgage
if($model->save()){   //保存 新增数据
$product_imgage->saveAs(‘./’.$product_imgage);//将上传的文件复制到指定目录,这个自己设置,这里上传的项目根目录。问了方便文件管理,建议这样使用  $product_imgage->saveAs(‘./assets/upload/’.$product_imgage) ,保证assets目录下存在upload目录
}
5 显示图片时,在view目录中,使用CHtm::image()函数
CHtml::image($model->product_imgage,//保存图片的名称,只要文件名正确 ,yii默认帮你查找图片
‘产品图片’, alt属性,放在页面显示的该名称
array(‘width’=>250,’height’=>120));  设置图片大小
附:
CUploadedFile
MethodDescriptionDefined 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()保存上传图片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值