Php – How to upload photo in yii

imagephpyii

i am not able to upload photo in my database and folder… this same code is working if i use CModel class for building form… but if i am using plain html it's giving me error Profilepic cannot be blank… my code is given below

my form

    <form method="post" action="<?php echo Yii::app()->getBaseUrl(true).'/index.php?r=user/create'?>" enctype="multipart/form-data">
<div class="row">
    <label>Username</label><input type="text" name="username"/><span><?php if(isset($error['username'])) echo $error['username'][0]; ?></span>
</div> 
<div class="row">
    <label>Password</label><input type="password" name="password"/><span><?php if(isset($error['password'])) echo $error['password'][0]; ?></span>
</div>
<div class="row">
    <label>Email</label><input type="text" name="email"/><span><?php if(isset($error['email'])) echo $error['email'][0]; ?></span>
</div>
<div class="row">
    <label>Profile pic</label><input type="file" name="profilepic"/><span><?php if(isset($error['profilepic'])) echo $error['profilepic'][0]; ?></span>
</div>
    <input type="hidden" value="No" name="flag"/>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

my controller method

   public function actionCreate()
{
    $model=new User;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST))
    {
                    $uploadedFile=CUploadedFile::getInstance($model,'profilepic');
                    $fileName = "{$uploadedFile}";  
                    $model->profilepic = $fileName;
        $model->attributes=$_POST;

                    if($model->save())
                    {
                        $a=$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$fileName);
                        $this->redirect(array('create'));
                    }
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

my rules method array

     return array(
        array('username, password,email,profilepic', 'required'),
        array('email','email'),
                    array('profilepic','file','types'=>'jpg,jpeg,png'),
                    array('email','unique'),
        array('username,password,email,flag,profilepic', 'safe', 'on'=>'search'),
    );

can somebody help me…

Thanks in advance

Best Solution

you need to change controller code like follow...

public function actionCreate()
{
   $model=new User;
   if(Yii::app()->request->isPostRequest) //change it
   {
                $model->profilepic=CUploadedFile::getInstanceByName('profilepic');
               // $fileName = "{$uploadedFile}"; //this is not required  
               // $model->profilepic = $fileName; //this is also not required
                $model->attributes=$_POST;

                if($model->save())
                {
                    $model->profilepic->saveAs(Yii::app()->basePath.'/../images/'.$model->profilepic);
                    $this->redirect(array('create'));
                }
   }

   $this->render('create',array(
       'model'=>$model,
   )); 
}

above all changes you need required... and one most important thing i changed is getInstanceByName('profilepic')

Hope it may solve your problem..