简体   繁体   中英

YII file upload not working

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM