5

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?

  • 3
    Note that those are warnings not errors, what you're trying to achieve isn't possible with WP_Filesystem – Tom J Nowell Mar 31 '18 at 1:06
2

No.

Keep in mind that the code sniffer is a limited utility operating on very simplistic parsing of the PHP code and has very little contextual knowledge. Therefor, you should use it as an advisory, a tool that should direct you to give a second thought to a part of your code, and not as some "source of truth" regarding code quality.

Once you have decided that your code is good, "mask" the error generation with one of the ways the sniffer provides, so it will not bugger you all the time and hide other problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.