php – Array-to-CSV-export功能在WordPress-plugin中面临一个问题

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Array-to-CSV-export功能在WordPress-plugin中面临一个问题脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的插件页面中使用了一个简单的数组到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',',');
我猜测在调用函数后WP正在进行正常操作,因此您将在CSV之后获得HTML模板输出.在fpassthru之后输入一个退出语句应该这样做,但是你需要小心,这不会搞乱wordpress在每个页面响应结束时所做的任何事情.例如,Drupal有一个drupal_exit()函数用于此目的.我不太熟悉WP以确定,但 wp_die() function的文档表明你可以使用PHP退出而没有太多问题

脚本宝典总结

以上是脚本宝典为你收集整理的php – Array-to-CSV-export功能在WordPress-plugin中面临一个问题全部内容,希望文章能够帮你解决php – Array-to-CSV-export功能在WordPress-plugin中面临一个问题所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。