Reading .csv file in php

前端 未结 8 932
面向向阳花
面向向阳花 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条回答
  •  再見小時候
    2020-12-16 15:31

    Try this....

    In PHP it is often useful to be able to read a CSV file and access it’s data. That is where the fgetcsv() function comes in handy, it will read each line of a CSV file and assign each value into an ARRAY. You can define the separator in the function as well see PHP docs for fgetcsv() for more options and examples.

      function readCSV($csvFile){
            $file_handle = fopen($csvFile, 'r');
            while (!feof($file_handle) ) {
                $line_of_text[] = fgetcsv($file_handle, 1024);
            }
            fclose($file_handle);
            return $line_of_text;
        }
    
    
        // Set path to CSV file
        $csvFile = 'test.csv';
    
        $csv = readCSV($csvFile);
        echo '
    ';
        print_r($csv);
        echo '
    ';

提交回复
热议问题