Hi all ,
I am trying to write an upload-image code.
I created an Image CActiveRecord model (and created a db table too…)
Now, I want to upload image from other controller. lets say from my userController.
I write this code in the actionCreate function:
public function actionCreate()
{
$model=new User;
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
$uploadedFile=CUploadedFile::getInstance($model,'image');
$model->image = Image::upload($uploadedFile)->id;
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
And I have this Image::upload function:
public static function upload($uploadedFile)
{
$image = new Image();
$image->code = md5(microtime().rand(1,9999).$uploadedFile->name);
$image->size = $uploadedFile->size;
$image->type = $uploadedFile->type;
$image->suffix = ".".$uploadedFile->getExtensionName();
if($image->save())
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$image->code.$image->suffix);
return $image;
}
return false;
}
And of course, I added a CHtml::activeFileField in my form…
What is wrong?
thaks!