Amit Singla
Amit Singla

Reputation: 313

get error "Fatal error: Call to a member function saveAs() on a non-object in controller" in “CMultiFileUpload ” in yii?

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

+

Upvotes: 0

Views: 3883

Answers (2)

Mete Yarıcı
Mete Yarıcı

Reputation: 170

and if using multiple file upload try this;

foreach ($files as $file) {
$files->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$fileName);    
}

Upvotes: 0

CreatoR
CreatoR

Reputation: 1652

Try:

$file->saveAs(Yii::getPathOfAlias('webroot').'/upload/productImage/'.$fileName);

Upvotes: 2

Related Questions