我正在使用“CMultiFileUpload”上传多个文件。我为此使用了以下代码:
我的视图代码是:
<?php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'product-images-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true
),
'htmlOptions' => array('enctype' => 'multipart/form-data')
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php
$this->widget('CMultiFileUpload', array(
'name' => 'image',
'attribute' => 'image',
'accept' => 'jpeg|jpg|png',
// 'max' => $max,
'duplicate' => 'file appears twice',
'remove' => Yii::t('ui', 'Remove'),
));
?>
<div class="row">
<?php echo $form->labelEx($model,'sortorder'); ?>
<?php echo $form->checkbox($model,'sortorder',array('size'=>60,'maxlength'=>300)); ?>
<?php echo $form->error($model,'sortorder'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Add' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
我的控制器代码是:
public function actionAddProductImages($id)
{
$model=new ProductImages;
if(isset($_POST['ProductImages']))
{
$count_files=count($_FILES['image']['name']);
for($i=0;$i<$count_files;$i++)
{
$rnd = rand(0,9999);
$model->attributes=$_POST['ProductImages'];
$uploadedFile = CUploadedFile::getInstance($model, "image" );
$fileName = "{$rnd}-{$uploadedFile}";
$model->image = $fileName;
$model->product_id = $id;
$model->sortorder = $_POST['ProductImages']['sortorder'];
if($model->save())
{
$uploadedFile->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName); // image will uplode to rootDirectory/banner/
//thumbmail---------------start---
Yii::app()->thumb->setThumbsDirectory('/upload/productImage/original/');
Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize(538,359)->save($fileName);
Yii::app()->thumb->setThumbsDirectory('/upload/productImage/thumb/');
Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize('0','110')->save($fileName);
Yii::app()->thumb->setThumbsDirectory('/upload/productImage/thumb_70/');
Yii::app()->thumb->load(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName)->resize('0',70)->save($fileName);
Yii::app()->user->setFlash('productImage','productImage has been added successfully');
$this->redirect(array('view','id'=>$model->image_id));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
现在的问题是:我无法获取文件名,我是 YII 的新手,请帮助我该怎么做。