最近,我制作了一个WordPress插件,并在其中提供了导出到csv的功能。我使用了php默认的fopen,fclose,它可以正常工作。但是,当我使用envato主题检查器检查插件时,建议您使用WP_Filesystem方法。但是我找不到打开csv文件并关闭而不保存到上载目录的方法。
if(isset($_POST['download_csv'])):
$filename = 'emails-' . time() . '.csv';
$header_row = array( 0 => 'Email', 1 => 'Date' );
$data_rows = array();
$users = $this->get_emails();
foreach( $users as $u ):
$row = array();
$row[0] = $u->email;
$row[1] = $u->dateTime;
$data_rows[] = $row;
endforeach;
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' );
$headerDisplayed = false;
foreach( $data_rows as $data ):
if( !$headerDisplayed ):
fputcsv($fh, $header_row);
$headerDisplayed = true;
<?php endif;
fputcsv($fh, $data);
endforeach;
fclose($fh);
exit;
endif;
?>
if(isset($_POST['download_csv'])):
$filename = 'emails-' . time() . '.csv';
$header_row = array( 0 => 'Email', 1 => 'Date' );
$data_rows = array();
$users = $this->get_emails();
foreach( $users as $u ):
$row = array();
$row[0] = $u->email;
$row[1] = $u->dateTime;
$data_rows[] = $row;
endforeach;
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' );
$headerDisplayed = false;
foreach( $data_rows as $data ):
if( !$headerDisplayed ):
fputcsv($fh, $header_row);
$headerDisplayed = true;
<?php endif;
fputcsv($fh, $data);
endforeach;
fclose($fh);
exit;
endif;
?>
一切正常。但是知道如何将其与WP_Filesystem集成吗?