Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在wordpress插件中强制下载文件_Php_Mysql_Wordpress_File_Csv - Fatal编程技术网

Php 在wordpress插件中强制下载文件

Php 在wordpress插件中强制下载文件,php,mysql,wordpress,file,csv,Php,Mysql,Wordpress,File,Csv,我很难创建一个.csv文件,并强制将其下载到wordpress插件中 如果我在php插件中调用这段代码,我会得到“不能修改标题”,并且从wordpress外部调用,我可以下载.csv,但不能包含wpdb.php(可能是因为我在插件文件夹之外没有访问权限) 对于这种情况,哪种方法更好?试试以下方法:-- 我在构建我的应用程序时遇到了同样的问题。我通过在插件的构造函数中使用if语句来解决这个问题,该语句将中断并输出csv 这就是我为自己所做的 function __construct( ){

我很难创建一个.csv文件,并强制将其下载到wordpress插件中

如果我在php插件中调用这段代码,我会得到“不能修改标题”,并且从wordpress外部调用,我可以下载.csv,但不能包含wpdb.php(可能是因为我在插件文件夹之外没有访问权限)


对于这种情况,哪种方法更好?

试试以下方法:--


我在构建我的应用程序时遇到了同样的问题。我通过在插件的构造函数中使用if语句来解决这个问题,该语句将中断并输出csv

这就是我为自己所做的

function __construct( ){
    if($_GET['export'] == true && $_GET['page'] == 'cwd-management' && is_admin())
    {
        $csv = '';
        foreach ($this->getAll() as $row) { // getAll() just returns all data in a custom table
            $csv .=  $row->ref . "," . $row->data . "\n";
        }

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"cwd-export_" . date("Y-m-d_H-i-s") . ".csv\";" );
        header("Content-Transfer-Encoding: binary");

        echo $csv;
        exit;
    }
}

在不了解表结构的情况下,我无法给出完整的示例,但希望您可以使用我的代码添加到插件类中。然后只需提供一个链接,该链接满足if station

它不起作用=(
无法修改标题信息-标题已由发送(输出从wp admin\menu header.php:107开始),然后从该文件中删除
ob_start();
,并添加
ob_start()
进入你的
wp config.php
文件…我不应该编辑wp配置,因为它将用于多个wordpress副本…别担心,
ob\u start()
没有其他效果…如果你使用
ob\u clean()
上周,我也遇到了同样的问题,我解决了这个问题……没关系,小姐,点击这里保存。现在它下载了,但它将整个html附加到了.csv中,没有它应该添加的字符串
<?php

ob_start();
ob_clean();

$fileName = 'somefile.csv'; 

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");

$fh = @fopen( 'php://output', 'w' );

global $wpdb;
$query = "SELECT nome, email FROM {$wpdb->prefix}coming_soon_lista_email";
$results = $wpdb->get_results( $query, ARRAY_A );
$headerDisplayed = false;

foreach ( unserialize($_GET['asd']) as $data ) {
    if ( !$headerDisplayed ) {
        fputcsv($fh, array_keys($data));
        $headerDisplayed = true;
    }
    fputcsv($fh, $data);
}
fclose($fh);
exit;
?>
function __construct( ){
    if($_GET['export'] == true && $_GET['page'] == 'cwd-management' && is_admin())
    {
        $csv = '';
        foreach ($this->getAll() as $row) { // getAll() just returns all data in a custom table
            $csv .=  $row->ref . "," . $row->data . "\n";
        }

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"cwd-export_" . date("Y-m-d_H-i-s") . ".csv\";" );
        header("Content-Transfer-Encoding: binary");

        echo $csv;
        exit;
    }
}