How to upload a file in the database

Hello for all,

How to upload a file in the database ?

That’s my code.

[color="#FF0000"]In controller (creat) :[/color]

$model=new Societes;












[color="#FF0000"]code of Model :[/color]

public function rules()


		return array(

			array('Nom', 'required'),

			array('Nom', 'length', 'max'=>75),

                        array('Logo', 'file', 'types'=>'jpg, gif, png'),

			array('Id, Nom, Logo', 'safe', 'on'=>'search'),



[color="#FF0000"]and the code of _form for add a new company with the pecture of logo :[/color]


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

		<?php echo $form->textField($model,'Nom',array('size'=>40,'maxlength'=>45)); ?>

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



 <?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

<?php echo CHtml::activeFileField($model, 'Logo'); ?>

<?php echo CHtml::endForm(); ?>


Thanks for all…

if you are using CActiveForm you can add enctype like that:

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






 <?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

<?php echo CHtml::activeFileField($model, 'Logo'); ?>

<?php echo CHtml::endForm(); ?>


change to


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

		<?php echo $form->fileField($model,'Logo',array('id'=>'product_image')); ?>

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


so what result of $_FILES in your controller (via var_dump())


I don’t understood what is mean that :

<?php echo $form->fileField($model,'Logo',array('id'=>'product_image')); ?>

I did all what you wrote but i have error in this line :


must add something about logo in model ?


post your database structure of table you are going to store the information

name of table is : societes

fields : id (int), name (varchar ) , description (varchar), logo (varchar )

so i need to upload pecture of logo

so can you help me plz ?

The logo field needs to be a BLOB, not a varchar.

but i need just the url of logo so i thought that varchar is better.

i changed now to blob but didn’t works and still the problem


Take a peek at the Yii Wiki:〈=

You are nearly there. ;)

You are saying that you want to save your image into database. I assumed that you want to store the image there that’s why i told you to use a BLOB type.

But if you want to save only the file name or url, the BLOB is no good, varchar is okay, so switch it back.

I used the following code to upload the images

In my Controller

			// Image Upload

			$image_h=$_POST['Gallery']['image_h']; // Hidden name of the image

			$image= CUploadedFile::getInstance($model,'image'); // Here 'image' is the name of field used to get Image 



			$image->saveAs('images/gallery/'.$image->getName()); // this is the directory where the images will be saved relative to application path





In _form.php

	<div class="row">

		<?php echo $form->labelEx($model,'image'); ?> // Label of the Image

        <?php echo '<table width="250"><tr valign="top"><td width=130>';





		echo '<img src="images/gallery/'.$image_name.'" width="120" height="120">'.'</td>';?> // This will display the current Image

		<?php echo '<td>'.$form->fileField($model,'image',array('id'=>'image')).'</td></tr></table>'; ?> // Field for File Selection

        <?php echo CHTML::hiddenField('Gallery[image_h]',$model->image); ?> // Hidden value to storing Old Image name

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


You can use this code and change it to fit your needs

Thank you PeRoChAk

Thanks friend

[color="#000080"][b]is still just one problem, the logo must be not empty for save new company or update it however is not required in rules.

really i don’t understood why must be the logo not empty.[/b][/color]

public function rules()


		return array(

			array('nom, active', 'required'),

			array('nom', 'length', 'max'=>75),

                        array('description', 'length', 'max'=>120),

                        array('logo', 'file', 'types'=>'jpg, gif, png'),


			array('id, nom, description, logo, active', 'safe', 'on'=>'search'),



Use “allowEmpty” property when logo isn’t required:


array(‘logo’, ‘file’, ‘types’ => ‘jpg,gif,png’, ‘allowEmpty’ => true)

Or define scenario if logo required on insert but not on update:

array(‘logo’, ‘file’, ‘types’ => ‘jpg,gif,png’, ‘on’ => ‘insert’)