Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9209041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:44:57+00:00 2026-06-18T00:44:57+00:00

I am trying to upload CSV file using an Yii framework. I have added

  • 0

I am trying to upload CSV file using an Yii framework. I have added the code below :

Model

<?php
    class Import extends CModel
    {
        public $name,$email,$doctor = 0,$hospital = 0,$diagnostic = 0;
        public function rules()
        {
            return array(
                array('name,email,doctor,hospital,diagnostic','required')
            );
        }

        public function  attributeNames()
        {
            return array(); 
        }
    }
?>

View(index.php)

<div class="form">
    <?php echo CHtml::beginForm('site/import',$method='post',$htmlOptions =array('enctype'=>'multipart/form-data')); ?>
        <div class="row">
            <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
            <input type="file" name="filename" value="file">
            <?php echo CHtml::submitButton('Submit', array('name'=>'submitCSV')); ?>
            <?php 
                if(is_array($buffer))
                {
                    foreach($buffer as $line){ 
                        echo '<div class="clear"></div>';
                        echo CHtml::activeTextField($buffer[0],'name',array('value',$line['name']));
                        echo CHtml::activeTextField($buffer[0],'email',array('value',$line['email']));
                        echo CHtml::activeTextField($buffer[0],'doctor',array('value',$line['doctor']));
                        echo CHtml::activeTextField($buffer[0],'hospital',array('value',$line['hospital']));
                        echo CHtml::activeTextField($buffer[0],'diagnostic',array('value',$line['diagnostic']));
            }}?>
        </div>
    <?php echo CHtml::endForm(); ?>
</div>

Controller

public function actionImport()
{
    $model = new Import;
    $buffer = array($model);
    if(isset($_POST['submitCSV'])){
        $filename=$_FILES['filename']['tmp_name'];
        $fp = fopen("$filename","r");
        if($fp){
            $i =0;
            while(($buffer[$i] = fgetcsv($fp,1000,",")) !== false)
            {
                $model = new Import;
                $model->name = $buffer[$i][0];
                $model->email = $buffer[$i][1];
                $model->doctor = $buffer[$i][2];
                $model->hospital = $buffer[$i][3];
                $model->diagnostic = $buffer[$i][4];
                $buffer[$i++] = $model;
            }
            if(!feof($fp))
                echo "Echo: Unexpected fgets() fail\n";
        }
        fclose($fp);
        unset($_POST['submitCSV']);
    }
    $this->render('index',array('buffer'=>$buffer));
}

Issues facing with this code :

  1. Since $line is an instance of Import, i should be able to use the elements of object $line but I am not able to do that. I used $line->'name' for this, but i got this error : Parse error: syntax error, unexpected ”name” (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘{‘ or ‘$’

  2. $line is a model here, so I should be able to place it as the first argument in the activeTextField statements. I get this error : get_class() expects parameter 1 to be object, boolean given

  3. Thirdly, this code generates same rows of text-field five times, so i thought of experimenting the activeTextField by placing $i in place of 0 in the code and incrementing it in the foreach loop. That creates the same error as in 2.

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:44:59+00:00Added an answer on June 18, 2026 at 12:44 am

    First of all use CFormModel instead of CModel. You may want to remove the attributeNames() function too.

    issue 1: $label->'name' is incorrect. use $label->name.

    issue 2 & 3 should work if you make changes as above

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to import a CSV file to my Rails app that's using CarrierWave
Am trying to upload CSV data to MYSQL table using the below query.It's running
I can't really read PHP but I'm trying to upload a csv file in
I am trying do some file upload using jquery. The upload box have to
I'm trying to upload a local CSV-file to Google Drive and display it like
I am trying to import data from a CSV file to a SQL Server
I'm trying to upload this CSV file onto my table in pgsql, but I'm
I am trying to do a simple upload of a .csv file and save
I am trying to upload a CSV file to Oracle database from my client
I am trying to upload a moderetly sized csv file to google app engine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.