Saving image use CUpLoadedFile

hello all…

Thanks before to read my post… i need anything help about saving image.

I did my code below by reading from :

if i have got miss understanding , i wish anyone tell me too…

I have a task to upload an image…

what i have done, i will tell below :

My Model:


public function rules()


return array(

array('', 'safe', 'on'=>'search'),


            'types'=>'jpg, gif, png, jpeg',

            'maxSize'=>1024 * 1024 * 50,

            'tooLarge'=>'The file was larger than 50MB. Please upload a smaller file.',





My Controller : ImageController.php

public function actionCreate()


	$model=new Image;




                    //$model->gambar = 'E:\Windows_Data\titi\Ling2\centil.jpg';



                    if($img === null)

                        echo 'KOSONG TERNYATA'; // Run this Statement ( $img is null )


                        $model->Path = file_get_contents($img->getTempName());




                        echo 'Name : '.$img->name;

                        echo 'Type : '.$img->type;

                        echo 'Size : '.$img->size;

                        echo 'Temp Name : '. $img->tempName;









My View : _form.php

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




)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;

&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;

&lt;div class=&quot;row&quot;&gt;

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Title'); ?&gt;

	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Title',array('size'=&gt;60,'maxlength'=&gt;200)); ?&gt;

	&lt;?php echo &#036;form-&gt;error(&#036;model,'Title'); ?&gt;


&lt;div class=&quot;row&quot;&gt;

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Path'); ?&gt;

	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Path',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;

	&lt;?php echo &#036;form-&gt;error(&#036;model,'Path'); ?&gt;


    &lt;?php //echo CHtml::form('','post',array('enctype'=&gt;'multipart/form-data')); ?&gt;

    &lt;?php echo CHtml::activeFileField(&#036;model, 'gambar'); ?&gt;

    &lt;?php //echo CHtml::endForm(); ?&gt;

&lt;div class=&quot;row buttons&quot;&gt;

	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


<?php $this->endWidget(); ?>

I very stuck with all of my experiment… can anyone help me to give me a clue or anything , to solve my problem here… I GOT $img STILL NULLL.

What I want to do is : i want to try to save an image into database with column type BLOB ( if there is another better way than what i did here, you can give me an alternatif too )…

help me plz… thanks before for all…

Thanks a lot

You need to uncomment the enctype declaration in your _form file.





Yes thanks blindMoe… this worked :rolleyes: