File upload in posts

Hi,

is use the blog demo and i want to one new filefield.

I have Item MVC from thistutorial.

This is my postcontroller, post model and post view.


   

if($model->save()) {

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

   $this->newItem();

   }

   $this->redirect(array('view','id'=>$model->id));

}

...  

            protected function newItem() {

            $item = new Item;

            $item->attributes=$_POST['Item'];

            $item->media=CUploadedFile::getInstance($item,'media');  

            var_dump($item->media);

            exit;

            $item->name=$item->media->name;

            $item->size=$item->media->size;

            if($item->save())

            {

                $item->media->saveAs('assets/media/'.$item->media->name);

                // redirect to success page

            }

            $post->addItem($item->media);


            return $item;

        }


        public function addItem($item)

	{

		$item->pid=$this->id;

		return $item->save();

	}


<div class="row">

            <?php $item = new Item; ?>

            <?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

            <?php echo CHtml::activeFileField($item, 'media'); ?>

        </div>

If is use the item MVC in standalone work it. What is the probleme?

Sorry for my poor english.

Well you don’t tell us what problem / error you get.

Anyway, have you declared in ‘media’ attribute in your Item model?


public $media;

And, also, have you tried the original code without changing it to see if it works?

I’ve personally had, and it works perfectly.

P.S. Ensure you have the necessary rights on the target directory (and that it exists, obviously).

/* Moved to General Discussion - not a Tip, Snippet or Tutorial */

I use the ‘enctype’=>‘multipart/form-data’, but not the right place.

I replace this row and it’s work. :)


<?php $form=$this->beginWidget('CActiveForm'); ?>

to


<?php $form=$this->beginWidget('CActiveForm', array('htmlOptions'=>array('enctype'=>'multipart/form-data'))); ?>

I forgot this options… :(