문제

I am using the "CMultiFileUpload " to upload multiple files. i have used the below code for this:

My view code is:

<?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 -->

My Controller Code is:

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,
    ));
}

Now what is the problem is: I am not able to get the file's name, i am new to YII, please help me how can i do this.

도움이 되었습니까?

해결책

<?php
/* @var $files CUploadedFile[] */
$files = CUploadedFile::getInstancesByName('image');
foreach ($files as $file)
{
    echo $file->getName();
}

다른 팁

Try CUploadedFile::getInstancesByName('image'), here link for example

For getting name, filename you may use:

$images = CUploadedFile::getInstancesByName('image');
foreach ($images as $image => $pic){
    $original_name = $pic->name;
    $path_to_uploaded = $pic->tempName;
}
$uploadedFile= CUploadedFile::getInstancesByName('image');
print_r($uploadedFile);
foreach ($uploadedFile as $photos){
   print "<br>Photo Name: "$photos->name;

}

use this

For getting name, filename you may use:

    $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);

  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top