Hi please check my following code:
Model:
public $image;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'file';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('image_url', 'required'),
array('image_url', 'length', 'max'=>256),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('image_url', 'safe', 'on'=>'search'),
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
Controler:
public function actionCreate()
{
$model=new File;
print_r($_POST['File']);
$this->render('create',array(
'model'=>$model,
));
}
View:
<?php
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image');
echo $form->error($model, 'image');
echo CHtml::submitButton('Submit');
$this->endWidget();?>
you can see print_r($_POST['File']); in my controller. It gives me empty array with no url or anything regarding the uploaded image. Please help
Thanks
Solution
following changes were made in the controller function which worked for me.
public function actionCreate()
{
$model=new File;
$model->image_url = CUploadedFile::getInstance($model,'image');
if($model->save())
$model->image_url->saveAs('D:/xampp/htdocs/project/images/upload/logo.jpg');
$this->render('create',array(
'model'=>$model,
));
}