簡體   English   中英

如何在 Laravel (PHP) 中讀取 CSV 文件緩沖區並將其轉換為 JSON?

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

我有以下代碼下載 .csv 文件。 這工作正常。

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"
        ]
    );
}

我需要讀取這個 .csv 緩沖區並將 CSV 文件轉換為 JSON 文件並將其下載為 JSON 文件。 所以首先,我嘗試將 .csv 轉換為 JSON 對象,如下所示:

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);
}

我將 CSV 帶到 $file 並使用 file_get_content 和其他 PHP 函數。 但是 'print_r($json);' 給我一個 NULL 值。

如何解決這個問題? 有沒有更好的方法以JSON格式下載生成的CSV?

先感謝您。

試一試吧

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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM