简体   繁体   中英

How to read CSV file buffer and convert it to JSON in Laravel (PHP)?

I have following code which downloads a .csv file. This works fine.

public function csv(Export $export)
{

    $buffer = $export->getCsvData_();

    return response()->streamDownload(
        function () use ($buffer) {
            $file = fopen('php://output', 'w');
            fputs($file, $buffer);
            fclose($file);
        },
        "$export->filename.$export->file_extension",
        [
            "Content-type" => "text/csv",
            "Content-Disposition" => "attachment; filename=$export->filename.$export->file_extension",
            "Pragma" => "no-cache",
            "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
            "Expires" => "0"
        ]
    );
}

I need to read this .csv buffer and convert the CSV file into a JSON file and download it as a JSON file. So first of all, I tried to convert .csv to JSON object like this:

public function json(Export $export)
{

    $buffer = $export->getCsvData_();

    $file = response()->streamDownload(
        function () use ($buffer) {
            $file = fopen('php://output', 'w');
            fputs($file, $buffer);
            fclose($file);
        },
        "$export->filename.csv",
        [
            "Content-type" => "text/csv",
            "Content-Disposition" => "attachment; filename=$export->filename.csv",
            "Pragma" => "no-cache",
            "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
            "Expires" => "0"
        ]
    );
    $csv = file_get_contents($file);
    print_r($file);
    $array = array_map("str_getcsv", explode("\n", $csv));
    $json = json_encode($array);
    print_r($json);
}

I'm taking the CSV to $file and using file_get_content and other PHP functions. But 'print_r($json);' gives me a NULL value.

How to fix this? Is there a better way to download the generated CSV in JSON format?

Thank you in advance.

Let's give it a try

public function json(Export $export)
    {
    
        $buffer = $export->getCsvData_();
    
        $file = response()->streamDownload(
            function () use ($buffer) {
                $file = fopen('php://output', 'w');
                fputs($file, $buffer);
                fclose($file);
            },
            "$export->filename.csv",
            [
                "Content-type" => "text/csv",
                "Content-Disposition" => "attachment; filename=$export->filename.csv",
                "Pragma" => "no-cache",
                "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
                "Expires" => "0"
            ]
        );
        //$file is the csv file URL
        $CSV_Data_Array = [];
        $file_handle = fopen($file, 'r');
        while (!feof($file_handle) ) {
           $CSV_Data_Array[] = fgetcsv($file_handle);
        }
        fclose($file_handle);
        //$csv = file_get_contents($file);
        print_r($CSV_Data_Array);
        //$array = array_map("str_getcsv", explode("\n", $csv));
        $json = json_encode($array);
        print_r($json);
    }

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