Reading .csv file in php

前端 未结 8 974
面向向阳花
面向向阳花 2020-12-16 15:12

I want to read .csv file in PHP and put its contents into the database. I wrote the following code:

$row = 1;
$file = fopen(\"qryWebsite.csv\", \"r\");
while         


        
8条回答
  •  旧时难觅i
    2020-12-16 15:24

    If you're using the composer package manager, you can also rely on league/csv

    According to theire documentation:

    use League\Csv\Reader;
    
    //load the CSV document from a file path
    $csv = Reader::createFromPath('/path/to/your/csv/file.csv', 'r');
    $csv->setHeaderOffset(0);
    
    $header = $csv->getHeader(); //returns the CSV header record
    $records = $csv->getRecords(); //returns all the CSV records as an Iterator object
    

提交回复
热议问题