I tried to upload file, using this tutorial. The problem is, that when I click “Save” in the form, the website just reloads and nothing happens. I tried to debug it and it doesn’t even call the actionCreate in the controller.
When I remove “‘htmlOptions’ => array(‘enctype’ => ‘multipart/form-data’),”, it gets to the controller, but the
...
public function actionCreate()
{
$model=new Version;
if(isset($_POST['Version']))
{
$model->attributes=$_POST['Version'];
$model->myfile=CUploadedFile::getInstance($model,'myfile');
if($model->save())
$model->myfile->saveAs(Yii::app()->baseUrl."/".$model->filename);
$this->redirect(array('view','id'=>$model->version));
}
$this->render('create',array(
'model'=>$model,
));
}
...
and the model:
class Version extends CActiveRecord
{
public $myfile;
...
public function rules()
{
return array(
array('filename', 'length', 'max'=>45),
array('myfile','file','types'=>'doc'),
...
public function actionCreate()
{
$model=new Banner; // this is my model related to table
if(isset($_POST['Banner']))
{
$rnd = rand(0,9999); // generate random number between 0-9999
$model->attributes=$_POST['Banner'];
$uploadedFile=CUploadedFile::getInstance($model,'image');
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->image = $fileName;
if($model->save())
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../banner/'.$fileName); // image will uplode to rootDirectory/banner/
$this->redirect(array('admin'));
}
}
$this->render('create',array(
'model'=>$model,
));
}[
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Banner']))
{
$_POST['Banner']['image'] = $model->image;
$model->attributes=$_POST['Banner'];
$uploadedFile=CUploadedFile::getInstance($model,'image');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../banner/'.$model->image);
}
$this->redirect(array('admin'));
}
}
$this->render('update',array(
'model'=>$model,
));
}
I have one doubt in image upload ,if click the upload button the browse file has to be opened in that if any image selected the image will be displayed instead of default image how do i do that task ?please help me
Thank you for your reply, the problem is, that I can’t even get to these actions when I debug. Only when I remove the “‘htmlOptions’ => array(‘enctype’ => ‘multipart/form-data’),”, I can get to the action.
Thanks for your reply
i have an another doubt
First , When i select image it should display in the popup window .
secondly , we need to crop the image and preview of the image must be display in the same popup window .
Third,when we click the submit button in the popup window, the cropped image must be stored in the corresponding folder and should replace the default image.