Hi!
I’m developing a web application trough which users must be able to upload images and see uploaded images.
Here is a code of my mode class:
==============================================================================================
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('etat', 'length', 'max'=>1),
//array('image', 'length', 'max'=>1024),
array('image', 'file', 'types'=>'jpg, gif, png'),
array('tbl_utilisateur_id, tbl_equipement_id', 'length', 'max'=>20),
array('date_maj', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('date_maj, etat, image, id, tbl_utilisateur_id, tbl_equipement_id', 'safe', 'on'=>'search'),
);
}
=============================================================================================================
in my controller I have :
=============================================================================================================
public function actionCreate()
{
$model=new EtatEquipement;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['EtatEquipement']))
{
$model->attributes = $_POST['EtatEquipement'];
$picture_file = CUploadedFile::getInstanceByName('imge');
// has the user uploaded a new file?
if($picture_file){
$picture_name = $picture_file->name;
$picture_file->SaveAs(Yii::app()->getBasePath().'\images\gallery' . $picture_name);
}else echo 'no picture file';
//save the model
if($model->save()){
// redirect to success page
$this->redirect(array('view','id'=>$model->id));
}else{
echo 'not saved';
}
}
$this->render('create',array(
'model'=>$model,
));
}
=============================================================================================================
And in my view I have
=============================================================================================================
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'etat-equipement-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<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,'etat'); ?>
<?php echo $form->dropDownList($model,'etat', array("F"=>"Fonctionnel", "D"=>"Défectueux", "R"=>"Rébuté")); ?>
<?php echo $form->error($model,'etat'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'image'); ?>
<?php /* echo $form->fileField($model,'image'); */ ?>
<?php echo CHtml::activeFileField($model, 'image'); ?><br>
<?php echo $form->error($model,'image'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
=============================================================================================================
When I execute the actionCreate(), the text ‘no picture file’ is displayed.
Please could anyone have/know a way to upload an image in Yii aaplication?
Thanks for your help.