【问题标题】:CSV Export from Wordpress | Sourcecode of page in file?从 Wordpress 导出 CSV |文件中页面的源代码?
【发布时间】:2019-02-06 08:56:41
【问题描述】:

我正在尝试从 wordpress 中的数据库生成 csv 文件。 生成的 CSV 文件包含生成的数据库数组和页面的 HTML 源代码。

知道摆脱 HTML 代码的解决方案是什么吗? ob_start() / ob_end_clean() 的策略;好像不行。

感谢您的帮助。

<?php

    ob_start(); 

        $filename = 'provider.csv';
        $headers = array('ID', 'Name', 'Location');

        $handle = fopen('php://memory', 'w'); 
        fputcsv($handle, $headers, ',', '"');

        $results = $wpdb->get_results("SELECT * FROM provider");

        foreach($results as $results1)
                {
            $row = array(
                $results1->provider_id,
                $results1->provider_name,
                $results1->provider_location
            );

            fputcsv($handle, $row, ',', '"');
        }

    ob_end_clean(); 

    fseek($handle, 0);

    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '";');

    fpassthru($handle);

    fclose($handle);


    ?>

已编辑:This is how the csv-file looks like

已编辑:Screenshot of the solution from aniket patel

【问题讨论】:

    标签: mysql wordpress csv export


    【解决方案1】:

    请使用下面的代码,我认为它对你有用。

        <?php
        global $wpdb;
        $filename = 'provider.csv';
        $headers = array('ID', 'Name', 'Location');
    
        $handle = fopen('php://output', 'w'); 
        fputcsv($handle, $headers, ',', '"');
    
        $results = $wpdb->get_results("SELECT * FROM provider");
    
        foreach($results as $results1)
                {
            $row = array(
                $results1->provider_id,
                $results1->provider_name,
                $results1->provider_location
            );
    
            fputcsv($handle, $row, ',', '"');
        }
    
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="' . $filename . '";');
        exit;
        ?>
    

    【讨论】:

    • 谢谢。不幸的是,CSV 文件仍然包含源代码
    • 请清除您的缓存,然后重试,因为它对我来说很好。
    • 我再次尝试清除缓存。但不幸的是,它仍然没有工作。但是有一件事发生了变化:数据库数据之后不再是页面的源代码。但是在数据库数据进来之前还是有的。 (看我的第二张截图链接)
    • 你确定你被替换了,这是我给的,因为我已经仔细检查了。
    • 是的,我做到了,并且如前所述,您的版本也发生了变化。现在剩下的问题是,在数据库中的实际数据出现之前,csv 文件中存在来自站点的代码。
    猜你喜欢
    • 2023-04-05
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多