0

I am not able to save the image uploaded from simple Form with this code

public function actionImage()
{
    print_r($_FILES);
    $dir = Yii::getPathOfAlias('application.uploads');

    if(isset($_POST['img']))
    {
        $model = new FileUpload();
        $model->attributes = $_POST['img'];
        $model->image=CUploadedFile::getInstance($model,'image');
        if($model->validate())
        {
            $model->image->saveAs($dir.'/'.$model->image->getName());
            // redirect to success page

        }
    }
}
Assad Ullah
  • 785
  • 6
  • 12

2 Answers2

1

To Answer my own question instead of using above code I used this:

public function actionImage()
{   
    $dir = Yii::getPathOfAlias('application.uploads');
    if (isset($_FILES['img']))
    {   
        $image = CUploadedFile::getInstanceByName('img');
        $image->saveAs($dir.'/'.$image->getName());
    }
}
Assad Ullah
  • 785
  • 6
  • 12
0

To Answer my own question instead of using above code I used this:

 public function actionImage() {   
    $dir = Yii::getPathOfAlias('application.uploads');
    if (isset($_FILES['img']))
    {   
        $image = CUploadedFile::getInstanceByName('img');
        $image->saveAs($dir.'/'.$image->getName());
    } }
jane
  • 11
  • 2
  • please refer to this link for more examples http://stackoverflow.com/questions/19092864/yii-file-upload-not-adding-to-database-using-form – jane Mar 04 '14 at 08:48