简体   繁体   English

为什么无法上传多个文件?

[英]Why Multiple File Upload Is Not Working?

I have tried so many ways to make multiple file upload workable using CMultiFileUpload widget. 我尝试了多种方法来使用CMultiFileUpload小部件上载多个文件。 Already, I have checked and followed below links- 我已经检查并关注了以下链接-

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

Yii multiple file upload Yii多文件上传

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. 当我使用GET方法查看表单提交功能时,在提交后我发现其显示文件名。 But, using POST method I did not see the name inside if(isset($_POST['Images'])) . 但是,使用POST方法时,在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. 多谢您的协助与协助。

In you code, change below line 在您的代码中,更改以下行

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM