File upload issue

Hi All,

I am trying to upload a single file, form posting everything well but posted file is missing. There is no instance while I’m trying to get through




$the_file = CUploadedFile::getInstance($model, 'uploaded_resume');

// This is also empty array

print_r($_FILES);



Please help me, what I’m missing or doing wrong.

You may check live test page at: vitalsmartsindia.com/vs/career/

Codes are as follow:

— CONTROLLER —




class CareerController extends Controller {


    public function actionIndex() {

        $model = new Careers;

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

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

            if ($model->validate()) {

                $the_file = CUploadedFile::getInstance($model, 'uploaded_resume');

                //echo "<pre>";

                //print_r($_FILES);

                //return;

                $tmpResume = Yii::getPathOfAlias('webroot.uploaded') . '/' . $model->name . "_" . $model->preferred_location . ($model->id + 1);

                // echo $the_file . " :<br/>" . $model->id . " :<br/>" . $tmpPicture;

                $the_file->saveAs($tmpResume);

                $model->uploaded_resume = $the_file;


                $model->save();


                $headers = "From: {" . Yii::app()->params['adminEmail'] . "\r\nReply-To: " . Yii::app()->params['adminEmail'];

                mail(Yii::app()->params['contactPost'], $model->preferred_location . " (" . $model->relevent_skills . ")", $model->message, $headers);

                Yii::app()->user->setFlash('contact', 'Thank you for job posting. We will respond to you as soon as possible.');

                $this->refresh();

            }

        }

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

    }


    public function actions() {

        return array(

            'captcha' => array(

                'class' => 'CCaptchaAction',

                'backColor' => 0xFFFFFF,

            ),

            'page' => array(

                'class' => 'CViewAction',

            ),

        );

    }

}



— MODEL —




/**

 * This is the model class for table "tbl_careers".

 *

 * The followings are the available columns in table 'tbl_careers':

 * @property string $id

 * @property string $post_applied

 * @property string $name

 * @property string $email_id

 * @property string $phone

 * @property string $preferred_location

 * @property string $relevant_skills

 * @property double $total_experience

 * @property string $uploaded_resume

 * @property string $alternate_resume

 * @property string $verifyCode

 */

class Careers extends CActiveRecord {


    public $verifyCode;


    public static function model($className=__CLASS__) {

        return parent::model($className);

    }


    public function tableName() {

        return 'tbl_careers';

    }


    public function rules() {

        return array(

            array('post_applied, name, email_id, phone, preferred_location, relevant_skills, total_experience, uploaded_resume', 'required'),

            array('total_experience', 'numerical'),

            array('post_applied, preferred_location', 'length', 'max' => 10),

            array('name, email_id', 'length', 'max' => 108),

            array('phone', 'length', 'max' => 15),

            array('relevant_skills, uploaded_resume', 'length', 'max' => 160),

            array('uploaded_resume', 'file', 'types' => 'doc,docx,odt,txt', 'on' => 'first_step'),

            array('email_id', 'email'),

            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements()),

            array('id, post_applied, name, email_id, phone, preferred_location, relevant_skills, total_experience, uploaded_resume, alternate_resume, date_time', 'safe', 'on' => 'search'),

        );

    }


    public function relations() {

        return array(

        );

    }


    public function attributeLabels() {

        return array(

            'id' => 'ID',

            'post_applied' => 'Post Applied',

            'name' => 'Name',

            'email_id' => 'Email',

            'phone' => 'Phone',

            'preferred_location' => 'Preferred Location',

            'relevant_skills' => 'Relevant Skills',

            'total_experience' => 'Total Experience',

            'uploaded_resume' => 'Upload your resume',

            'file_types' => 'doc,docx,odt,txt',

            'alternate_resume' => 'Alternate Text Resume',

            'alternate_resume_' => 'Or paste your text resume here',

            'verifyCode' => 'Verification Code',

        );

    }


    public function getCareerPosts() {

        return CHtml::listData(CareerPosts::model()->findAll(array("order" => "name", "condition" => "active = '1' AND deleted = '0'")), 'id', 'name');

    }


    public function getCity() {

        return CHtml::listData(City::model()->findAll(array("order" => "name", "condition" => "active = '1' AND deleted = '0'")), 'id', 'name');

    }


    public function search() {

        $criteria = new CDbCriteria;


        $criteria->compare('id', $this->id, true);

        $criteria->compare('post_applied', $this->post_applied, true);

        $criteria->compare('name', $this->name, true);

        $criteria->compare('email_id', $this->email_id, true);

        $criteria->compare('phone', $this->phone, true);

        $criteria->compare('preferred_location', $this->preferred_location, true);

        $criteria->compare('relevant_skills', $this->relevant_skills, true);

        $criteria->compare('total_experience', $this->total_experience);

        $criteria->compare('uploaded_resume', $this->uploaded_resume, true);

        $criteria->compare('alternate_resume', $this->alternate_resume, true);


        return new CActiveDataProvider($this, array(

                    'criteria' => $criteria,

                ));

    }


}



VIEW:




<?php

        $form = $this->beginWidget('CActiveForm', array(

            'id' => 'suggestion-form',

            'enableClientValidation' => true,

            'clientOptions' => array(

                'validateOnSubmit' => true,

            ),

                ));

        ?>

            <!--

                Something hiding to minimize ...

            -->

            <div class="row deco">

                <?

                $Labels = $model->attributeLabels();

                echo

                $form->labelEx($model, 'uploaded_resume') .

                $form->fileField($model, 'uploaded_resume') .

                " <small>Only accepts: <b>" . $Labels['file_types'] . "</b></small>" .

                $form->error($model, 'uploaded_resume')

                ?>

            </div>


            <div class="row deco">

                <?

                echo

                $form->labelEx($model, 'alternate_resume') .

                $form->textField($model, 'alternate_resume') .

                " <b>" . $Labels['alternate_resume_'] . "</b>" .

                $form->error($model, 'alternate_resume')

                ?>

            </div>

            <!--

               more not included ...

            -->

        <?php $this->endWidget() ?>



Could be some PHP setting… check your php.ini settings for these keys

post_max_size - should be bigger than the size of the file you want to upload

file_uploads - should be on

upload_max_filesize - should be bigger than the size of the file you want to upload

max_file_uploads - should be more than 0

upload_tmp_dir - folder where files are uploaded initialy - check web process permissions to this folder

First thanks for your reply

My php setting is as follows:




post_max_size = 8M

file_uploads = On

upload_max_filesize = 2M

max_file_uploads = 20

# Note: /var/www/tmp folder is writable with permission 0777

upload_tmp_dir = /var/www/tmp



I don’t think, there is anything missing in server side configuration. I did something wrong with Yii code.

you may check my configuration at: http://vitalsmartsindia.com/php-info.php




        $form = $this->beginWidget('CActiveForm', array(

            'id' => 'suggestion-form',

            'enableClientValidation' => true,

            'clientOptions' => array(

                'validateOnSubmit' => true,

            ),

            'htmlOptions'=>array(

                'enctype'=>'multipart/form-data',

            ),

        ));



Try to remove the ‘on’ => ‘first step’ in the validation rule…

Thanks you all members helping me,

I did all that things you suggested, now upload problem is solved by adding formWidget htmlOptions[enctype=multipart/form-data]. And I also removed ‘on’=>‘first_step’ from validation rule. But this is still validating as Upload your resume cannot be blank.

I’m new for Yii FW and never used any FW before Yii.

Check the allowEmpty property - http://www.yiiframework.com/doc/api/1.1/CFileValidator#allowEmpty-detail