Getting Error While Saving/uploading Image

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

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 function




 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 rule function 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 anybody help me…

Hi,

You are getting this error because of this condition

array(‘username, password,email,profilepic’, ‘required’),

You should be using this:




        $uploadedFile=CUploadedFile::getInstanceByName('profilepic');

        $model->profilepic = $uploadedFile;



Thanks…

worked like charm… one more thing i want to ask you that… in my html form i am printing errors… it’s working fine but problem is whenever i am opening it first time at that time also it’s printing error say Username can’t be blank… how to hide it ?

Change




if(isset($_POST))



to




if (Yii::app()->request->isPostRequest)



Ohh great… thank you very much Mr. Keith :) :)