0

我正在使用“CMultiFileUpload”在 YII 中上传多个文件。我正在使用以下代码:

public function actionAddProductImages($id)
{       
    $model=new ProductImages;
    if(isset($_POST['ProductImages']))
    {   
                $model->attributes=$_POST['ProductImages'];
                $files = CUploadedFile::getInstancesByName('image');                                                            
                foreach ($files as $file)
                {
                        //$rnd = rand(0,9999);                                                                                  
                        $fileName = $file->getName();                                                   
                        $model->image = $fileName;
                        $model->product_id = $id;
                        $model->sortorder = $_POST['ProductImages']['sortorder'];                       
                        if($model->save())
                        {                                   
                            //$files->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName); // image will uplode to rootDirectory/banner/   
                            $file->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName);
                             //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,
    ));
}

实际上这段代码会发生什么:它只将最后一个文件的信息保存在数据库中,并将所有选定的文件上传到指定的文件夹中。请帮我找出我错的地方。我是 YII 的新手。

4

1 回答 1

1

尝试(新图像 - 数据库中的新行,这就是为什么新模型):

public function actionAddProductImages($id)
{
    if(isset($_POST['ProductImages']))
    {
        $files = CUploadedFile::getInstancesByName('image');
        foreach ($files as $file)
        {
            $model=new ProductImages;
            $model->attributes=$_POST['ProductImages'];
            //$rnd = rand(0,9999);                                                                                  
            $fileName = $file->getName();
            $model->image = $fileName;
            $model->product_id = $id;
            $model->sortorder = $_POST['ProductImages']['sortorder'];
            if($model->save())
            {
                //$files->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName); // image will uplode to rootDirectory/banner/   
                $file->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName);
                //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,
    ));
}
于 2013-10-31T13:57:35.157 回答