Create and download a clean CSV using PHP in a CMS

134 Views Asked by At

I'm using Concrete5 CMS and I'm trying to allow users to download a subset of other users (ones they invited).

To do this, I am making a new page and using the following:

  $new_csv = fopen('members.csv', 'w');
  $array = array("foo", "bar", "hello", "world"); // For testing
  fputcsv($new_csv, $array);
  fclose($new_csv);

  header("Content-type: text/csv");
  header("Content-disposition: attachment; filename = members.csv");
  readfile("members.csv");

The problem I am having is this is writing the whole page. Is there a way to do this possibly without using headers? My approach is probably entirely off, so if there is a better way to achieve this, please let me know.

1

There are 1 best solutions below

0
biplob On

You may use Core::make('app')->shutdown(); to fix this issue.

But for doing the export in a better way- Check the \Concrete\Controller\SinglePage\Dashboard\Users\Search::csv_export() method. It would be quite similar. Just set your filtered user list to $writer->insertList($list) method.