Uploading File

Hi,

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


CUploadedFile::getInstance($model,'myfile')

returns null.

This is my view:




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'version-form',

	'enableAjaxValidation'=>false,

        'htmlOptions' => array('enctype' => 'multipart/form-data'),


)); ?>

...

<div class="row">

	<?php echo $form->labelEx($model,'myfile'); ?>

        <?php echo $form->fileField($model, 'myfile'); ?>

        <?php echo $form->error($model,'myfile'); ?>

</div>

...



Controller:




...

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'),

                        ...






What am I missing?

Hi,

please see the from action what should be generated?

Hi,

sorry I don’t understand you - do you mean form action? But the action which is called is actionCreate, isn’t it?

Hi,

Please got though this

http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

this my code works fine, compare with your code

controller


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,

        ));

    }









Hi,

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

what you mean by default image…?? Can explain clearly.

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.

I’ve already tried that tutorial, see my first post.

default profile image -before user update their image.

Put this on view page. ie if there no value is model a default image will be shown, else the updated image will been shown





<?php if($model->image==''){ ?>

<div class="row">

     <?php echo CHtml::image(Yii::app()->request->baseUrl.'/banner/default.jpg',"image",array("width"=>200)); ?>  // Image that default shows

</div>

<?php } else { ?>

<div class="row">

     <?php echo CHtml::image(Yii::app()->request->baseUrl.'/banner/'.$model->image,"image",array("width"=>200)); ?>  // Image shown here if page is update page

</div>


<?php } ?>






You can also specify action




"'htmlOptions' => array('enctype' => 'multipart/form-data','action'=>Yii::app()->createUrl('controller/action')),", I can get to the action. 



Thank you for your reply

Hi good morning,

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.

For that you have try you self with some available extensions yii crop

go throgh

http://www.yiiframework.com/extension/jii-jcrop/

http://www.yiiframework.com/extension/jcrop/

http://www.yiiframework.com/extension/imagecropper/

Thank you

Thank u interboy.

I’ve tried it with no luck. I’ve found out that when the multipart/form-data is set, there’s no content in $_POST or $_FILE. How is it possible?

Hi,

How can i use image like (+) plus image for upload button instead of input file button for multiple file uploader.

is there any way to do this?? If yes, please suggest me.

I want to display image for upload file.

Thanks.

I think u want like this,

In ur form.php

<script type="text/javascript">

function uploadImage() {

&#036;(&quot;#pro_pic&quot;).click(); 


return false; 

}

</script>

<img src="ur (+)img path" onclick="return uploadImage();"/>

<div style="display:none;">

<?php echo $form->textfield($model,‘image’,array(‘id’=>‘pro_pic’));?>

</div>

Try…