【问题标题】:File empty after fputcsvfputcsv 后文件为空
【发布时间】:2016-10-31 15:51:27
【问题描述】:

我用 fputcsv 创建了一个文件

    $handle = fopen('php://output', 'w+');

    // Add the header of the CSV file
    fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
    // Query data from database

    // Add the data queried from database
    foreach ($results as $result) {
        fputcsv(
            $handle, // The file pointer
            array(...), // The fields
            ';' // The delimiter
        );
    }

    file_put_contents('mycsv.csv',fgetcsv($handle), FILE_APPEND);

    fclose($handle);
 ....

我想将输出保存到 mycsv.csv 但文件为空

【问题讨论】:

    标签: php csv fputcsv


    【解决方案1】:

    php://outputstream,其工作方式类似于 echoprint。这意味着,您正在编写标准输出(可能是您的控制台或浏览器)。

    如果您想将内容写入 csv 文件,请尝试打开此文件或直接使用 PHP 创建它,而不是使用 file_put_contents

    $handle = fopen("mycsv.csv","wb");
    fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
    //...put your code
    rewind($handle);//optional.it will set the file pointer to the begin of the file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多