0

我正在使用“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 的新手,请帮助我该怎么做。

4

4 回答 4

1
<?php
/* @var $files CUploadedFile[] */
$files = CUploadedFile::getInstancesByName('image');
foreach ($files as $file)
{
    echo $file->getName();
}
于 2013-10-31T09:42:28.723 回答
0

试试,例如CUploadedFile::getInstancesByName('image')这里的链接

要获取名称、文件名,您可以使用:

$images = CUploadedFile::getInstancesByName('image');
foreach ($images as $image => $pic){
    $original_name = $pic->name;
    $path_to_uploaded = $pic->tempName;
}
于 2013-10-31T09:35:59.850 回答
0

用这个

要获取名称、文件名,您可以使用:

    $sfile=CUploadedFile::getInstances($model, $attribute){

    foreach ($sfile as $i=>$file){
    $fileName = "{$sfile[$i]}";// This Gives File name form multiple files
    $formatName=time().$i.'_'.$fileName;
    $file->saveAs(Yii::app()->basePath.$path.$formatName);

  }
于 2014-02-18T13:04:49.943 回答
0
$uploadedFile= CUploadedFile::getInstancesByName('image');
print_r($uploadedFile);
foreach ($uploadedFile as $photos){
   print "<br>Photo Name: "$photos->name;

}
于 2014-02-04T13:43:39.300 回答