in protected/components I have
<?php
class upload extends CWidget
{
public $dir=NULL;
public function run()
{
// this method is called by CController::endWidget()
$form = new UploadForm;
if (isset($_POST['UploadForm'])) {
if ($form->validate()) {
$form->image = CUploadedFile::getInstance($form, 'image');
$file= dirname(Yii::app()->request->scriptFile).DIRECTORY_SEPARATOR.$this->dir.$form->image->name;
$form->image->saveAs($file);
}
}
$this->render('upload', array('form'=>$form,'dir'=>$this->dir));
}
}
?>
in protected/components/views I have
<!--protected/views/site/upload.php-->
<div class="yiiForm">
<?php echo CHtml::form('', 'post', array('enctype'=>'multipart/form-data')); ?>
<?php echo CHtml::errorSummary($form); ?>
<div class="simple">
<?php echo CHtml::activeLabel($form,'image'); ?>
<?php echo CHtml::activeFileField($form, 'image'); ?>
<br/>
<?php echo CHtml::submitButton('Upload'); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div>
in protected/models/ I have
<?php
// protected/models/UploadForm.php
class UploadForm extends CFormModel {
public $image;
public function rules() {
return array(
array('image', 'file', 'types' => 'pdf, doc'),
);
}
}
?>
then from any view I can cal my widget with this line
<?php $this->widget('application.components.upload',array('dir'=>'docs/results/day6/')); ?>
and I can make the content of ‘dir’ whatever I want and it will work. it will upload the file to whatever folder I point it too.
The problems happens when I call the widget more than once in one view, as in:
<?php $this->widget('application.components.upload',array('dir'=>'docs/results/day6/')); ?>
<?php $this->widget('application.components.upload',array('dir'=>'docs/results/day5/')); ?>
I get 2 forms in my view and regardless which one I use all the files go to the directory set in the first call (in this example they would all go to docs/results/day6/)
that’s what I’m trying to figure out… .why it won’t take the setting from the next widget call??