0

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.

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
Amit Singla
  • 313
  • 1
  • 7
  • 18

4 Answers4

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

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;
}
CreatoR
  • 1,654
  • 10
  • 14
  • i have tried it as below print $uploadedFile = CUploadedFile::getInstancesByName('image'); it shows me "Array" as output.please suggest what to do next.as i want to get the filename. – Amit Singla Oct 31 '13 at 09:40
  • try use `print_r($uploadedFile);` and you see the structure of array OR you can go to the link and see how work with this array and get need data – CreatoR Oct 31 '13 at 09:42
0
$uploadedFile= CUploadedFile::getInstancesByName('image');
print_r($uploadedFile);
foreach ($uploadedFile as $photos){
   print "<br>Photo Name: "$photos->name;

}
0

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

  }
sandeep
  • 73
  • 1
  • 7