【问题标题】:Array-to-CSV-export function is facing an issue in WordPress-pluginArray-to-CSV-export 函数在 WordPress-plugin 中遇到问题
【发布时间】:2015-07-01 05:02:52
【问题描述】:

我在我的插件页面中使用了一个简单的数组到 CSV 导出功能来生成报告。

当我运行此代码时,我收到一个错误,它将导出整个 html 内容以及我预期的数组。

这是我的代码:

function convert_to_csv($input_array, $output_file_name, $delimiter)
{
    clearstatcache();
    /** open raw memory as file, no need for temp files */
    $temp_memory = fopen('php://memory', 'w');
    /** loop through array  */



    foreach ($input_array as $line) {
        /** default php csv handler **/
        fputcsv($temp_memory, $line, $delimiter);
    }

    //echo '<pre>';
    //print_r($temp_memory); exit;
    /** rewrind the "file" with the csv lines **/
    fseek($temp_memory, 0);
    /** modify header to be downloadable csv file **/
    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
    /** Send file to browser for download */
    fpassthru($temp_memory);
}

/** Array to convert to csv */

$array_to_csv = Array(
    Array(12566,
        'Enmanuel',
        'Corvo'
    ),
    Array(56544,
        'John',
        'Doe'
    ),
    Array(78550,
        'Mark',
        'Smith'
    )

);

clearstatcache();

convert_to_csv($array_to_csv, 'report.csv', ',');

【问题讨论】:

  • 我在运行此代码时没有收到错误消息。我按预期下载了 csv。
  • 当我在 WordPress 的插件环境中运行此代码时,我得到了 csv,它显示了列表中的所有 html 代码
  • 代码按预期运行,并下载了一个 csv 文件。添加有关插件和 Wordpress 版本的信息,以帮助我们解决这个问题。
  • 您是否启用了任何类型的调试(WP_DEBUG 设置为 true 或 XDebug)?如果是这样,这可能会导致您的 CSV 文件呈现无关的 HTML。

标签: php arrays wordpress csv


【解决方案1】:

我猜在调用此函数后 WP 正在继续其正常操作,因此您将在 CSV 之后获得 HTML 模板输出。在fpassthru 之后放入exit 语句应该可以做到这一点,但是您需要注意这不会弄乱Wordpress 在每个页面响应结束时所做的任何其他事情。例如 Drupal 有一个 drupal_exit() 函数就是为了这个目的。我对 WP 不太熟悉,无法确定,但wp_die() function 的文档建议您可以使用 PHP exit 而不会出现太多问题

【讨论】:

    【解决方案2】:

    您可以使用此代码生成扩展名为 .xlsx 的 excel 文件

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script>
    
    <script type="text/javascript">
        $(document).ready(function() {
        var insuranceArray = new Array(insuranceInfo);
        var bills = db.bills;
    
        //Create tabs in excel file
        var opts = [{sheetid:'Insurance Information',header:true},{sheetid:'bills Info',header:false}];
    
        alasql('SELECT * INTO XLSX("Data.xlsx",?) FROM ?', [opts, insuranceArray,bills]]);
    
        // Close the window once you save the file
        window.onfocus=function(){ 
            window.close();
        }
    </script>
    

    此代码对我有用...

    【讨论】:

      【解决方案3】:

      在导出函数的顶部试试这个。

          global $wpdb;
          $wpdb->hide_errors();
          @set_time_limit(0);
          if ( function_exists( 'apache_setenv' ) )
              @apache_setenv( 'no-gzip', 1 );
          @ini_set('zlib.output_compression', 0);
      
          //@ob_clean();
          @ob_end_clean(); // to prevent issue that unidentified characters when opened in MS-Excel in some servers
      
      
              header( 'Content-Type: text/csv; charset=UTF-8' );
              header( 'Content-Disposition: attachment; filename=export.csv' );
              header( 'Pragma: no-cache' );
              header( 'Expires: 0' );
      
              $fp = fopen('php://output', 'w');
      

      【讨论】:

        【解决方案4】:

        生成 csv 的最简单方法,无需将文件保存在服务器上

        <?php
        $array = Array(
            Array(
                12566,
                'Enmanuel',
                'Corvo'
            ),
            Array(
                56544,
                'John',
                'Doe'
            ),
            Array(
                78550,
                'Mark',
                'Smith'
            )
            
        );
        arr_to_csv($array);
        function arr_to_csv($array)
        {
            header("Content-type: application/csv");
            header("Content-Disposition: attachment; filename=test.csv");
            $fp = fopen('php://output', 'w');
            foreach ($array as $row) {
                fputcsv($fp, $row);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-09-16
          • 2020-05-11
          • 1970-01-01
          • 2021-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多