0

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;
        }   
    }
?>
PrakashG
  • 1,642
  • 5
  • 20
  • 30
Jacky
  • 771
  • 3
  • 20
  • @RiggsFolly i am asking about how to upload csv file but you mark as wrong duplicate – Jacky Jul 26 '19 at 10:16
  • So that means that the object variable, is not an object. So some thing went wrong in the code before the line in the error message. _Big Note_ If you have errors in code, we cannot help unless we can see the code. So please post code in the future – RiggsFolly Jul 26 '19 at 10:16
  • Where do you instantiate the reader onto the variable (object property) `Csvreader` – RiggsFolly Jul 26 '19 at 10:21
  • I can't understand – Jacky Jul 26 '19 at 10:22
  • You have this line `$result = $this->Csvreader->parse_file('$filePath');` that assumes there is an object instantiated onto the class property `$this->Csvreader` where do you instantiate `CI_CsvReader` onto the property `Csvreader` – RiggsFolly Jul 26 '19 at 10:23
  • hello this is library file need not to consider CI_csvReader in codeiginter framwork – Jacky Jul 26 '19 at 10:25
  • This message `Call to a member function parse_file() on null` means that the Object you are calling the method `parse_file()` on, is null. i.e. It is not an object – RiggsFolly Jul 26 '19 at 10:28
  • okey but in that library file has a function as parse_file() – Jacky Jul 26 '19 at 10:29
  • i have attached link also for reference see that also – Jacky Jul 26 '19 at 10:29
  • Yes, but you have to instantiate a class into an object, something like `$obj = new ClassName();` somewhere before you can actually make use of the class – RiggsFolly Jul 26 '19 at 10:30
  • where i have to instantiate? – Jacky Jul 26 '19 at 10:31
  • Somewhere before you try and use it – RiggsFolly Jul 26 '19 at 10:32

2 Answers2

2

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 .

Swarna Sekhar Dhar
  • 550
  • 1
  • 8
  • 25
1

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);
PrakashG
  • 1,642
  • 5
  • 20
  • 30
Jacky
  • 771
  • 3
  • 20