为什么多文件上传不起作用?

时间:2014-02-05 06:24:51

标签: php yii multifile-uploader cmultifileupload

我已经尝试了很多方法,可以使用CMultiFileUpload小部件使多个文件上传工作。我已经检查并按照以下链接 -

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

Yii multiple file upload

但仍然没有运气!!

这是我的代码:

查看:

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

控制器:

当我使用GET方法查看表单提交功能时,我发现它在提交后显示文件名。但是,使用POST方法我没有在if(isset($_POST['Images']))中看到名称。即使我在那里也看不到回声结果。

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

模型规则:

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

我的代码有什么问题?

感谢您的帮助&amp;善意的合作。

1 个答案:

答案 0 :(得分:0)

在您的代码中,更改以下行

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

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