Voici ce que ça devrait donner :
Code SQL pour la table :
CREATE TABLE IF NOT EXISTS `tbl_societes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`nom` varchar(30) NOT NULL,
`description` varchar(100) NOT NULL,
`logo` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
_form.php:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'htmlOptions'=>array('enctype' => 'multipart/form-data'),
'id'=>'societes-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'nom'); ?>
<?php echo $form->textField($model,'nom',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'nom'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'logo'); ?>
<?php echo $form->fileField($model,'logo'); ?>
<?php echo $form->error($model,'logo'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Societes.php:
...
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('nom, description', 'required'),
array('nom', 'length', 'max'=>30),
array('description, logo', 'length', 'max'=>100),
array('logo', 'file', 'types'=>'jpg, gif, png'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, nom, description, logo', 'safe', 'on'=>'search'),
);
}
...
SocietesController.php:
...
public function actionCreate()
{
$model=new Societes;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Societes']))
{
$model->attributes=$_POST['Societes'];
$file=CUploadedFile::getInstance($model,'logo');
$extension = $file->getExtensionName();
$filename = md5_file($file->tempName);
$fullFilename = $filename . '.' . $extension;
$model->logo=$fullFilename;
if($model->save())
{
$file->saveAs(Yii::app()->basePath . '/../images/' . $fullFilename);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
...
(les logos seront stockés dans un dossier images situé au même niveau que protected, avec un nom correspondant au md5 du fichier uploadé.