Why Multiple File Upload Is Not Working?

349 Views Asked by At

I have tried so many ways to make multiple file upload workable using CMultiFileUpload widget. Already, I have checked and followed below links-

http://www.yiiframework.com/forum/index.php/topic/47665-multiple-file-upload/

Yii multiple file upload

BUT still no luck!!

Here is my code:

View:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'images-form',
'method'=>'post',
'enableAjaxValidation'=>false,
'enableClientValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
 )); ?>

<?php
$this->widget('CMultiFileUpload', array(
'model'=>$model,
'name'=>'image_filename',
'attribute'=>'image_filename',
'accept'=>'jpg|gif|png',
'options'=>array(
    // 'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }',
    // 'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }',
    // 'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }',
    // 'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }',
    // 'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }',
    // 'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }',
),
'denied'=>'File is not allowed',
'max'=>10,
'htmlOptions' => array( 'multiple' => 'multiple'),
));
?>

<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Add' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>

Controller:

When I use GET method to see the form submit functionality then I found its displaying file name after submission. But, using POST method I did not see the name inside if(isset($_POST['Images'])). Even I failed see echo result inside there.

public function actionPhoto()
{            
  $model=new Images;
  if(isset($_POST['Images']))
  {
    echo 1;//Test purpose. Not displaying after form submission!!

    $model->attributes=$_POST['Images'];

    $image_filename = CUploadedFile::getInstancesByName('image_filename');

    // proceed if the images have been set
    if (isset($image_filename) && count($image_filename) > 0)
    {
        // go through each uploaded image
        foreach ($image_filename as $image_filename => $pic)
        {
            echo $pic->name.'<br />';//Check purpose

            if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/'.$pic->name))
            {  
                $Images = new Images();
                $Images->image_filename = $pic->name;

                $Images->save();
            }
        }
    }
}

$this->render('photo',array('model'=>$model));
}

Model rules:

public function rules()
{
  return array(
    array('image_filename', 'file', 'types'=>'jpg, jpeg, png, gif','allowEmpty'=>false, 'on'=>'insert'),       
    array('id, image_filename', 'safe'),
    array('id, image_filename', 'safe', 'on'=>'search'),
);
}

What is wrong in my code?

Thanks for your help & kind co-operation.

1

There are 1 best solutions below

9
On

In you code, change below line

$image_filename = CUploadedFile::getInstancesByName('$image_filename');

to

$image_filename = CUploadedFile::getInstancesByName('image_filename');