0

i want to upload the multiple files using "CMultiFileUpload" in YII, when i am trying to run the below code i got an error "Fatal error: Call to a member function saveAs() on a non-object in controller".

public function actionAddProductImages($id)
{       
    $model=new ProductImages;
    if(isset($_POST['ProductImages']))
    {   
                $files = CUploadedFile::getInstancesByName('image');
                foreach ($files as $file)
                {
                        //$rnd = rand(0,9999);
                        $model->attributes=$_POST['ProductImages'];
                        $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/                                                 
                             //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,
    ));
}

+

4

2 回答 2

2

尝试:

$file->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName);
于 2013-10-31T10:38:13.697 回答
0

如果使用多个文件上传试试这个;

foreach ($files as $file) {
$files->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$fileName);    
}
于 2016-03-30T08:55:33.783 回答