phplaravelcsvmaatwebsite-excel

PHP Laravel read csv


I have a CSV file where the data is in landscape orientation.ex:

name, test name
age, 20
gender,Male

where the first column is the headers and the second the data, i tried using laravel maatwebsite/Excel and the response after reading the file, the first row is taken as the headers. (name and test name).

is there any method to read this type of CSV files in laravel using maatwebsite/Excel


Solution

  • You can use this function

    public function readCSV($csvFile, $delimiter = ',')
    {
        $file_handle = fopen($csvFile, 'r');
        while ($csvRow = fgetcsv($file_handle, null, $delimiter)) {
            $line_of_text[] = $csvRow;
        }
        fclose($file_handle);
        return $line_of_text;
    }
    $csvFileName = "test.csv";
    $csvFile = public_path('csv/' . $csvFileName);
    $this->readCSV($csvFile,array('delimiter' => ','))