1

Hi I am attempting to upload a file and write it to the database using YII, but nothing is happening at all, Its neither saving the file nor name saving to DB.

My View...

<div class="row">
        <div class="span4"><?php echo $form->labelEx($model,'slider_image'); ?></div>
        <div class="span5"><?php echo $form->fileField($model,'slider_image'); ?></div>
        <div class="span3"><?php echo $form->error($model,'slider_image'); ?></div>
</div>

My Model for validation...

    public function rules()
{

    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        //more rules
        array('slider_image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true),
        //more rules
    );
}

Controller:

    public function actionEdit()
{

    $id = Yii::app()->getRequest()->getQuery('id');

    $model = CustomPage::model()->findByPk($id);
    if (!($model instanceof CustomPage))
    {
        Yii::app()->user->setFlash('error',"Invalid Custom Page");
        $this->redirect($this->createUrl("custompage/index"));

    }
    if(isset($_POST['CustomPage']))
    {

        $model->attributes = $_POST['CustomPage'];

        if (CUploadedFile::getInstance($model,'slider_image')) {

        $model->slider_image=CUploadedFile::getInstance($model,'slider_image');

        }


        if ($model->validate())
        {
            if ($model->deleteMe)
            {
                $model->delete();
                Yii::app()->user->setFlash('info',"Custom page has been deleted");
                $this->redirect($this->createUrl("custompage/index"));
            }
            else {
                $model->request_url =  _xls_seo_url($model->title);
                if (!$model->save())
                    Yii::app()->user->setFlash('error',print_r($model->getErrors(),true));
                else
                {
                    if (CUploadedFile::getInstance($model,'slider_image'))  {
                    $model->slider_image->saveAs(Yii::app()->baseUrl.'images/'.$model->slider_image);
                    }

                    Yii::app()->user->setFlash('success',
                        Yii::t('admin','Custom page updated on {time}.',array('{time}'=>date("d F, Y  h:i:sa"))));
                    $this->beforeAction('edit'); //In case we renamed one and we want to update menu

                }
            }
        }

    }
    $this->render('edit',array('model'=>$model));

}

I attempted to die; after if (CUploadedFile::getInstance($model,'slider_image')) and nothing is happening, so it seems its not recognising it at all.

Thank you.

4
  • check print_r($_FILES) on actionEdit on top. Commented Jan 7, 2014 at 11:11
  • no error unfortunately Commented Jan 7, 2014 at 11:29
  • print_r($_FILES) gives me an empty array Commented Jan 7, 2014 at 11:29
  • Yes I got the image name by doing this, very strange that its not recognising anything with CUploadedFile Commented Jan 7, 2014 at 11:41

1 Answer 1

2

I think you're missing a minor directive in your view

Check to confirm that your form tag has the attribute "enctype"

i.e. <form action="" method="post" enctype="multipart/form-data">...</form>

TO set this in CActiveForm, do:

<?php $form = $this->widget('CActiveForm', array(
        'htmlOptions'=>array('enctype'=>'multipart/form-data')
));?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, this fixed the issue immediately, I never realized this was necessary, next time I need to make sure to include more code
You're welcome. I fall victim to it most times too (even though I know it's required for file uploads)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.