PHP Laravel read csv

Naveed Sheriffdeen picture Naveed Sheriffdeen · Oct 23, 2018 · Viewed 18.3k times · Source

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

Answer

Sagar Patel picture Sagar Patel · Oct 23, 2018

HI Naveed Sheriffdeen,

You Can This function

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