How to use CSV import library in CodeIgniter?

社会主义新天地 提交于 2019-12-11 16:32:09

问题


I have load csv library in my project folder. Using this link.
But I don't know where we should keep the csv file to upload and I got below error.

error:

Call to a member function parse_file() on null

My Controller code:

function index()
{
    $this->load->library('Csvreader');
    $filePath = './E:/Test.csv';  
    $result =   $this->Csvreader->parse_file('$filePath');//path to csv file

    $data['csvData'] =  $result;
    $this->load->view('view_csv', $data);  
}

My library Code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    class CI_CsvReader {

        var $fields;            /** columns names retrieved after parsing */ 
        var $separator  =   ';';    /** separator used to explode each line */
        var $enclosure  =   '"';    /** enclosure used to decorate each field */

        var $max_row_size   =   4096;    /** maximum row size to be used for decoding */

        function parse_file($p_Filepath) 
        {
            $file           =   fopen($p_Filepath, 'r');
            $this->fields   =   fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
            $keys_values        =   explode(',',$this->fields[0]);

            $content            =   array();
            $keys           =   $this->escape_string($keys_values);

            $i  =   1;
            while(($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) 
            {
                if( $row != null ) { // skip empty lines
                    $values         =   explode(',',$row[0]);
                    if(count($keys) == count($values)){
                        $arr            =   array();
                        $new_values =   array();
                        $new_values =   $this->escape_string($values);
                        for($j=0;$j<count($keys);$j++){
                            if($keys[$j]    !=  ""){
                                $arr[$keys[$j]] =   $new_values[$j];
                            }
                        }
                        $content[$i]    =   $arr;
                        $i++;
                    }
                }
            }
            fclose($file);
            return $content;
        }

        function escape_string($data)
        {
            $result =   array();
            foreach($data as $row){
                $result[]   =   str_replace('"', '',$row);
            }
            return $result;
        }   
    }
?>

回答1:


Have you tried accessing from temp location . i mean use the code as follow

$result =   $this->Csvreader->parse_file($_FILES['file_field_name']['tmp_name']);

by this you can save server space by not storing the file if you don't have any requirement to store it .




回答2:


The library function is correct but i made mistake in controller and i didn't define the path of the file correctly. My Answer is :

$this->load->library('csvreader');
$result =   $this->csvreader->parse_file('E:\Test.csv');
$data['csvData'] =  $result;
$this->load->view('view_csv', $data);


来源:https://stackoverflow.com/questions/57217875/how-to-use-csv-import-library-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!