Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 660
Naveed Sheriffdeen Avatar asked Oct 23 '18 11:10

Naveed Sheriffdeen


1 Answers

You can use 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' => ','))
like image 155
Sagar Patel Avatar answered Sep 24 '22 01:09

Sagar Patel