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;


		if(isset($_POST['Societes']))

		{

			$model->attributes=$_POST['Societes'];

                        $model->logo=CUploadedFile::getInstance($model,'Logo');

			if($model->save())

                                $model->logo->saveAs('path/images');

				$this->redirect(array('view','id'=>$model->Id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

[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]


<div>

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

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

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

	</div>


  <div>

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


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


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

</div>

Thanks for all…

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


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

	'id'=>'produk-form',

	'enableAjaxValidation'=>false,

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

delete


<div>

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


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


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

</div>

change to


<div>

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

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

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

	</div>

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

-xent

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 :


$model->logo=CUploadedFile::getInstance($model,'Logo'); 

must add something about logo in model ?

freeyii

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

Thanks

Take a peek at the Yii Wiki:

http://www.yiiframework.com/search/?q=file&type=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 

		

			if(!empty($image)){

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

					

			$image_h=$image->getName();

			}

			$model->image=$image_h;



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>';

		$image_name='no-image.jpg'; 

		if(!empty($model->image)){

		$image_name=$model->image;

		}

		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'); ?>

	</div>



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('active','length','max'=>10),

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

		);

	}

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

[code]

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’)