Skip to main content
edited tags
Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459
Tweeted twitter.com/StackWordPress/status/979915073899651073
Source Link
user3574603
  • 341
  • 1
  • 2
  • 11

Do I need to use WP_Filesystem when creating a downloadable file on the fly?

I have a method that creates a downloadable CSV on the fly when a button is clicked:

header( 'Content-type: text/csv' );
header( 'Cache-Control: no-store, no-cache' );
header( 'Content-Disposition: attachment; filename="email_list.csv"' );

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

fputcsv( $outstream, $csv->header );

foreach ( $csv->rows as $row ) {
    fputcsv( $outstream, $row );
}

fclose( $outstream );

exit();

When I run the class file through PHP Code Sniffer I get the following warnings:

 63 | WARNING | File operations should use WP_Filesystem methods instead of
    |         | direct PHP filesystem calls. Found: fopen()
 71 | WARNING | File operations should use WP_Filesystem methods instead of
    |         | direct PHP filesystem calls. Found: fclose()

As I understand it WP_Filesystem is necessary when dealing with files on the server. I would like to know if it is necessary when creating downloadable files on the fly like my example above. If so, why?