我有一个自定义插件可将特定数据库表导出为CSV文件。
问题是我没有任何“下载”选项...这只是在屏幕上写文件..我无法下载。
这是代码
function Code_CSV_export_page() {
if( !current_user_can( 'manage_options' ) ){ return false; }
if( !is_admin() ){ return false; }
if (isset($_POST['export']))
{
global $wpdb, $ID_angebot;
$ID_angebot = $_POST['ID_angebot_enter'];
$filename = date("Ymd") . '-' . 'Export-Angebot-' . $ID_angebot . '.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );
$header_row = array(
0 =>'ID ANGEBOT',
1=>'ANGEBOT',
2 =>'ENDDATUM',
3 =>'FIRMENNAMEN',
4 =>'CODE',
5 =>'VERWENDET',
6 =>'TITEL',
7 =>'NACHNAME',
8 =>'VORNAME',
9 =>'HERUNTERLADEN DATUM'
);
$data_rows = array();
$query = $wpdb->prepare("SELECT * FROM wpPGRmaplus_angebote WHERE `id_angebot`=$ID_angebot ");
$results = $wpdb->get_results($query);
foreach ( $results as $result ) {
$row = array();
$row [0] = $result -> id_angebot;
$row [1] = $result -> angebot_name;
$row [2] = $result -> enddatum;
$row [3] = $result -> firmenname;
$row [4] = $result -> code;
$row [5] = $result -> verwendet;
$row [6] = $result -> Titel;
$row [7] = $result -> Nachname;
$row [8] = $result -> Vorname;
$row [9] = $result -> herunterladen_datum;
$data_rows[] = $row;
}
//fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
// header avant ici
fputcsv( $fh, $header_row );
foreach ( $data_rows as $data_row ) {
fputcsv( $fh, $data_row );
}
fclose( $fh );
die();
}
?>
<div class="wrap">
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>" >
<table class="form-table">
<tbody>
<tr><th> </br> </br> </th> </tr>
<tr><th> </br> </br> </th> <td style="padding-bottom:60px; font-size: 25px; font-weight:bold;">Exportcodes und ihre Benutzer </td> </tr>
<tr><th> </br> </th> </tr>
<tr><th>ID des Angebots bezogen auf die Codeliste</th>
<?php $ID_angebot_enter = $custom_angebotenter["ID_angebot_enter"][0]; ?>
<td style="padding-top:20px;" ><input class="regular-text" id="ID_angebot_enter" type="text" name="ID_angebot_enter" value="<?php echo $ID_angebot_enter;?>" /></br>
<i> <?php echo 'Die ID wird im Angebot angezeigt -> Teil "Wie benutzt man das Angebot in Wordpress?"'; ?></i> <br/>
<i> <?php echo 'ACHTUNG, Export der Datenbank, abhängig vom gewählten Angebot;' ?></i> <br/> <br/> <br/> </td>
</tr>
<?php //récupération de l'id de l'offre
$ID_angebot = $_POST['ID_angebot_enter'];?>
<tr><th> Export in Excel File</th>
<td><input type="submit" name="export" value="EXPORT TO CSV"></td>
</tr>
</tbody>
</table>
</form>
</div>
<?php } ?>
我经常搜索并尝试了很多东西。 我什么都看不见......我需要一个外面的眼睛......
你能帮我吗?
非常感谢您的帮助
Timama
答案 0 :(得分:1)
你必须把下载代码放在init hook上:
`add_action( 'init', 'downloadfunc' );
function downloadfunc() {
if(isset($_POST['export'])) {
//download xode goes here
}
}`
答案 1 :(得分:0)
好吧,我试过了钩子。但是我认为我的钩子不好; - )
所以,这里现在有效的代码; - )
function Code_CSV_export_page() {
if(is_admin()){
global $wpdb;
if(isset($_POST['exportbtn'])){
$ID_angebot = $_POST['ID_angebot_enter'];
if (empty($_POST['ID_angebot_enter']))
{ ?>
<table class="form-table">
<tbody>
<tr><th> </br> </br> </th> </tr>
<tr><th> </br> </th> </tr>
<tr>
<th style="color:red";>VORSICHT</th>
<td style="color:red";> - Bitte, nicht vergessen die Angebots-ID - </td>
</tr>
<tr><th> </br> </th> </tr>
<tr>
<th> <a href="https://programm-maplus.de/wp-admin/edit.php?post_type=angebote&page=Code_CSV_export"> Zur Export Plugin</a></th>
<td> </td>
</tr>
<?php exit();
}
ob_clean();
$fh = @fopen( 'php://output', 'w' );
$header_row = array(
0 =>'ID ANGEBOT',
1 =>'ANGEBOT',
2 =>'ENDDATUM',
3 =>'FIRMENNAMEN',
4 =>'CODE',
5 =>'VERWENDET',
6 =>'TITEL',
7 =>'NACHNAME',
8 =>'VORNAME',
9 =>'HERUNTERLADEN DATUM'
);
$data_rows = array();
$query = $wpdb->prepare("SELECT * FROM wpPGRmaplus_angebote WHERE `id_angebot`=$ID_angebot ");
$results = $wpdb->get_results($query);
foreach ( $results as $result ) {
$row = array();
$row [0] = $result -> id_angebot;
$row [1] = $result -> angebot_name;
$row [2] = $result -> enddatum;
$row [3] = $result -> firmenname;
$row [4] = $result -> code;
$row [5] = $result -> verwendet;
$row [6] = $result -> Titel;
$row [7] = $result -> Nachname;
$row [8] = $result -> Vorname;
$row [9] = $result -> herunterladen_datum;
$data_rows[] = $row;
}
fputcsv( $fh, $header_row,"\t", '"' );
foreach ( $data_rows as $data_row ) {
fputcsv( $fh, $data_row ,"\t", '"');
}
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=".date('Ymd')."-"."Export-Angebot-".$ID_angebot.".xls"); //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
}
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" id="export-form">
<table class="form-table">
<tbody>
<tr><th> </br> </br> </th> </tr>
<tr><th> </br> </br> </th> <td style="padding-bottom:60px; font-size: 25px; font-weight:bold;">Exportcodes und ihre Benutzer </td> </tr>
<tr><th> </br> </th> </tr>
<tr><th>ID des Angebots bezogen auf die Codeliste</th>
<?php $ID_angebot_enter = $custom_angebotenter["ID_angebot_enter"][0]; ?>
<td style="padding-top:20px;" ><input class="regular-text" id="ID_angebot_enter" type="text" name="ID_angebot_enter" value="<?php echo $ID_angebot_enter;?>" /></br>
<i> <?php echo 'Die ID wird im Angebot angezeigt -> Teil "Wie benutzt man das Angebot in Wordpress?"'; ?></i> <br/>
<i> <?php echo 'ACHTUNG, Export der Datenbank, abhängig vom gewählten Angebot;' ?></i> <br/> <br/> <br/> </td>
</tr>
<tr><th> Export in Excel File</th>
<td> <input class="button button-primary exportbtn" name="exportbtn" type="submit" name="table_display" value="Export"/></td>
</tr>
</tbody>
</table>
</form>
<?php } }