file uploading

I have a view (actually a few views) where I need to include some file uploading capabilities.

in my view I have the following sections

  • press release

  • technical documents

  • daily results

each one of those needs to have an instance of the file uploader linked to it so that I can send the content to the appropriate directory structure. (i.e. documents/press, documents/tech and documents/results/day)

I made this script work

http://blog.mbischof.de/mit-yii-eine-datei-hochladen

is there a way to call the upload script from another view?

right now the upload scripts sits in this directory

protected/views/site/upload.php

and the view I want to call it from is in

protected/views/event/results.php

I tried using renderPartial from within results.php but it didn’t work. it did however work when I copied this code (this was in SiteController) right inside the view as a test only




public function actionUpload() {

	    $form = new UploadForm;

    	if (isset($_POST['UploadForm'])) {

	        if ($form->validate()) {

    	        $form->file = CUploadedFile::getInstance($form, 'file');

        	    $file= dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . $form->file->name;

        	    

            	$form->file->saveAs($file);

        	}

    	}

    	$this->render('upload', array('form'=>$form));

	}

	



I must be close since I can get it work but i’m sure there’s a fancier way of doing this than me copying the contents of that actionUpload function everytime I want to use it

would creating a widget be better? if so, how would you go about creating one based on the code I have from the blog linked above?

Pabs

>> would creating a widget be better?

widget is what i would do if I need it in more than one place

what error does it give you when you renderPartial

well I don’t get an error per say. it works but in my view (result.php) I need to paste the content of the actionUpdate that resides in SiteControler. if I don’t do that it complains about functions not begin defined cause it never sees the code in SiteControler. I tried putting that code in my eventControler but that didn’t seem to work.

and yes, I will be using this in various places throughout my site… which is why I don’t want to do paste that code in every single view I want to use it

made the widget work

here’s what I did. might be a cleaner way to this, if so, please share

my widget is here:

in protected/components/upload.php




class upload extends CWidget

{

    public $dir;

	

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

    }

}




then in protected/views/site/upload.php 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>



and my model is here protected/models/UploadForm.php




class UploadForm extends CFormModel {

    public $image;

 

    public function rules() {

        return array(

            array('image', 'file', 'types' => 'jpg, gif, png'),

        );

    }

}



now I can call this from any view using the following


 $this->widget('application.components.upload',array('dir'=>'docs/results/day4/')); 

only problem I have now is that if I call multiple instances of my widget it does not properly

if I do this for instance


 

$this->widget('application.components.upload',array('dir'=>'docs/results/day4/')); 

$this->widget('application.components.upload',array('dir'=>'docs/results/day5/'));

$this->widget('application.components.upload',array('dir'=>'docs/results/day6/'));




also you need to add the components in the protected/config/main.php




....

	'import'=>array(

[indent]	'application.models.*',

	'application.components.*',

	'application.components.views.*',

        'application.modules.user.models.*',

        'application.modules.user.components.*',

[/indent]	),


...



I can upload but all the files go to the directory in the first call (in this case docs/results/day4/)

not sure why it’s not resetting that parameter. any ideas?