File upload in posts


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'])) {






            protected function newItem() {

            $item = new Item;










                // redirect to success page



            return $item;


        public function addItem($item)



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


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


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

I forgot this options… :(