简体   繁体   中英

Generate a CSV file from XML stream for MYSQL Import

I am trying to get data from a online XML file and save in CSV format. I was able to save the CSV file but the data is not saved properly as a comma separated CSV and due to which the data has no delimiter and is not getting imported in mysql properly. It imports in mysql in one column itself where there are 8 columns and it should be imported in different columns as per CSV data.

I am using

        $report = stream_get_contents($request->getReport());
            // output headers so that the file is downloaded rather than displayed
            header('Content-type: text/csv');
            header('Content-Disposition: attachment; filename="data.csv"');

            // do not cache the file
            header('Pragma: no-cache');
            header('Expires: 0');
                $file = fopen('data.csv','w+');
                     fwrite($file,$report);
                     fclose($file);             

You are writing the lines without formatting them properly first. You should use fputcsv instead of fwrite .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM