yii框架提供了activeFileField控件来完成上传文件(当然也包括了上传图片)的操作,下面介绍yii的activeFileField使用方法。

1、函数原型:
public static string activeFileField( $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)最后,需要在控制类里加上下面的:
$p_w_picpath=CUploadedFile::getInstance($model,’BookImg’);
if (is_object($p_w_picpath) && get_class($p_w_picpath)===’CUploadedFile’)
{
$model->BookImg=’D:/aaa/aaa.jpg’; //请根据自己的需求生成相应的路径,但是要记得和下面保存路径保持一致
}
else
{
$model->BookImg=’NoPic.jpg’;
}
if($model->save())
{
if (is_object($p_w_picpath) && get_class($p_w_picpath)===’CUploadedFile’)
{
$p_w_picpath->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 echoCHtml::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)最后,需要在控制类里加上下面的:
$p_w_picpath=CUploadedFile::getInstance($model,’BookImg’); if (is_object($p_w_picpath) && get_class($p_w_picpath)===’CUploadedFile’) { $model->BookImg=’D:/aaa/aaa.jpg’; //请根据自己的需求生成相应的路径,但是要记得和下面保存路径保持一致 } else { $model->BookImg=’NoPic.jpg’; } if($model->save()) { if (is_object($p_w_picpath) && get_class($p_w_picpath)===’CUploadedFile’) { $p_w_picpath->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::p_w_picpath()函数
CHtml::p_w_picpath($model->product_imgage,//保存图片的名称,只要文件名正确 ,yii默认帮你查找图片
‘产品图片’, alt属性,放在页面显示的该名称
array(‘width’=>250,’height’=>120)); 设置图片大小
附:
CUploadedFile
METHOD DESCRIPTION DEFINED BY
Calls the named method which is not a class method.
Returns a property value, an event handler list or a behavior based on its name.
Checks if a property value is null.
Sets value of a component property.
String output. CUploadedFile
Sets a component property to be null.
Returns the named behavior object.
Attaches a behavior to this component.
Attaches a list of behaviors to the component.
Attaches an event handler to an event.
Determines whether a property can be read.
Determines whether a property can be set.
Detaches a behavior from the component.
Detaches all behaviors from the component.
Detaches an existing event handler.
Disables an attached behavior.
Disables all behaviors attached to this component.
Enables an attached behavior.
Enables all behaviors attached to this component.
Returns an error code describing the status of this file uploading. CUploadedFile
Returns the list of attached event handlers for an event.
  CUploadedFile
  CUploadedFile
Returns an instance of the specified uploaded file. CUploadedFile
Returns an instance of the specified uploaded file. CUploadedFile
Returns all uploaded files for the given model attribute. CUploadedFile
Returns an array of instances for the specified array name. CUploadedFile
  CUploadedFile
  CUploadedFile
  CUploadedFile
  CUploadedFile
Determines whether an event is defined.
Checks whether the named event has attached handlers.
Determines whether a property is defined.
Raises an event.
保存上传图片