Yii 1.1: Upload CSV File

9 followers

View file

       $form=$this->beginWidget('CActiveForm', array(
        'id'=>'registration-form',
         'enableAjaxValidation'=>true,
             'htmlOptions' => array('enctype' => 'multipart/form-data'),
        )); 

            <div>
        <?php echo $form->labelEx($model,'csv_file'); ?>
        <?php echo $form->fileField($model,'csv_file'); ?>
        <?php echo $form->error($model, 'csv_file'); ?>
    </div>
        <hr>
        <?php  echo CHtml::submitButton('Upload CSV',array("class"=>"")); ?>
        <?php echo $form->errorSummary($model); ?>
    </div>

        $this->endWidget();

Model file

            public function rules()
        {
                array('csv_file',
           'file', 'types' => 'csv',
           'maxSize'=>5242880,
           'allowEmpty' => true,
           'wrongType'=>'Only csv allowed.',
           'tooLarge'=>'File too large! 5MB is the limit'),
          }
          public function attributeLabels()
      {
              'csv_file'=>'Upload CSV File',
          }

Controller file

public function actionImport() {

    $model = new Registration;
    //$file = CUploadedFile::getInstance($model,'csv_file');
    if(isset($_POST['Registration']))
    {
        $model->attributes=$_POST['Registration'];
        if(!empty($_FILES['Registration']['tmp_name']['csv_file']))
        {
            $file = CUploadedFile::getInstance($model,'csv_file');


            $fp = fopen($file->tempName, 'r');
            if($fp)
            {
                //  $line = fgetcsv($fp, 1000, ",");
                //  print_r($line); exit;
                $first_time = true;
             do {
                if ($first_time == true) {
                    $first_time = false;
                    continue;
                }
                    $model = new Registration;
                    $model->firstname = $line[0];
                    $model->lastname  = $line[1];

                    $model->save();

                }while( ($line = fgetcsv($fp, 1000, ";")) != FALSE);
                $this->redirect('././index');

            }
            //    echo   $content = fread($fp, filesize($file->tempName));

        }



    }


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

}

Total 2 comments

#18319 report it
1nstinct at 2014/10/15 03:31am
About error above
public function rules() {
        return array(
            array(
                'csv_file',
                'file',
                'types' => 'xls',
                'maxSize' => 5242880,
                'allowEmpty' => true,
                'wrongType' => 'Only csv allowed.',
                'tooLarge' => 'File too large! 5MB is the limit'
            )
        );
    }
#15663 report it
mi.sarah at 2013/12/05 05:09am
model file needs return array

the model file needs to return an array

public function rules() { 
        return array('csv_file', 'file', 'types' => 'csv', 
            'maxSize'=>5242880, 
            'allowEmpty' => true, 
            'wrongType'=>'Only csv allowed.', 
            'tooLarge'=>'File too large! 5MB is the limit');                
} 
 
public function attributeLabels() { 
        return array('csv_file'=>'Import CSV',); 
}

Leave a comment

Please to leave your comment.

Write new article
  • Written by: bhavesh vaghela
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +7
  • Viewed: 10,171 times
  • Created on: Jan 9, 2013
  • Last updated: Oct 15, 2014