【问题标题】:How to read content of CSV file in codeigniter如何在codeigniter中读取CSV文件的内容
【发布时间】:2018-02-22 10:36:50
【问题描述】:

我已经使用 multipart-form 将 CSV 上传到 codeigniter,这是 HTML 代码:

<label for="enterCSV">Enter CSV            
<input  type="file"  name="enterCSV" accept=".csv">
</label>

这是控制器上的 php 代码:

  $csv = $_FILES['enterCSV']['name'];

            $file = fopen($csv, r); //open file
            while (!feof($file)){ //read till the end of the file
                $content = fgetcsv($file); //get content of the file
            }

我只想使用我的 php 读取上传的 csv 文件的内容并进行相应的操作。

【问题讨论】:

  • 请提及您所面临的确切问题,顺便说一下应该是$_FILES['enterCSV']['tmp_name']

标签: php codeigniter csv


【解决方案1】:

你可以试试这个:

$csv = $_FILES['file']['tmp_name'];

$handle = fopen($csv,"r");
while (($row = fgetcsv($handle, 10000, ",")) != FALSE) //get row vales
{
    print_r($row); //rows in array

   //here you can manipulate the values by accessing the array


}

【讨论】:

    【解决方案2】:

    这是我使用的代码: 在视图中:

    <form action="<?php echo base_url('Controller/get_content'); ?>"  method="POST" enctype="multipart/form-data">
        <input type="file" class="custom-file-input" id="csv_file" name="csv_file"> 
        <label class="custom-file-label" for="csv_file">Choose CSV file</label>
        <button class="btn btn-success" type="submit"> Import</button>
    </form>
    

    在控制器上:

    public function get_content(){
        $csv = $_FILES['csv_file']['tmp_name'];
        $handle = fopen($csv,"r");
        while (($row = fgetcsv($handle, 10000, ",")) != FALSE)
        {
            //row should have same structure as database
            $add = $this->model->add($row);
        }
                    
        redirect('/import_page', 'refresh');
    }
    

    在模型上:

    public function add($data){
       return $this->db->insert("table_csv", $data);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 2016-11-02
      • 2021-12-25
      • 2011-12-12
      • 2014-11-28
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      相关资源
      最近更新 更多