Uploading file and recording the filename


I am having a strange problem with activeFileField. I want to upload a file and record the filename.

First, I just put in the activeFileField in the form, editing the form generated by the CRUD generator, without actually uploading the file, and without putting the ‘htmlOptions’=>array(‘enctype’=>‘multipart/form-data’).

	<div class="row">

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

		<?php //echo $form->textField($model,'photo_leaf',array('size'=>60,'maxlength'=>255)); ?>


		echo CHtml::activeFileField($model, 'photo_leaf'  	);


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


The model receives the filename just fine, it is recorded.

Then I want to do uploading. I just put in ‘htmlOptions’=>array(‘enctype’=>‘multipart/form-data’), like so in the view file _forms, which I’m told is needed to upload a file. I didn’t do anything else.

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




)); ?>

Suddenly I can’t record the filename.

And if I put in the rest of the uploading code, I can upload the file I want, but can’t get the filename recorded. I am confused.

	public function actionCreate()


		$model=new Samples;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);












				$model->leaf_image->saveAs('images/'. $model->sample_id . '/'.$model->leaf_image);










What I want to do is to upload the file and record the filename, but I’ve been puzzling about it for two days. I can do one or the other but not both.

Please help. Did I do something wrong? Is my Yii corrupted?

This is part of the model:

class Samples extends CActiveRecord



	 * Returns the static model of the specified AR class.

	 * @return Samples the static model class



	public $leaf_image; 


	public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(



			array('species_id, sensor_id, sample_type_id, soil_id, param1_id, param2_id, param3_id, param4_id, param5_id', 'numerical', 'integerOnly'=>true),



			array('sample_code', 'length', 'max'=>30),

			array('photo_leaf, spectrum_file', 'length', 'max'=>255),

			array('photo_canopy', 'length', 'max'=>100),

			array('comment, spec_data', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('sample_id, sample_code, species_id, sensor_id, sample_type_id, soil_id, comment, photo_leaf, photo_canopy, spectrum_file, spec_data, param1_id, param2_id, param3_id, param4_id, param5_id', 'safe', 'on'=>'search'),



I am allowing the photo_leaf to be null input in the database, before generating the model with Gii and the CRUD generator.

in short, thats the way

$image = CUploadedFile::getInstance($model,'photo_leaf');

$model->leaf_image= $image->name;


That was not in the tutorial