I want to generate a csv file in var folder. I used these lines of code it’s not working. Can someone help me to figure it out
public function execute()
{
$filepath = 'export/customerlist.csv';
$this->directory->create('export');
$stream = $this->directory->openFile($filepath, 'w+');
$stream->lock();
$header = ['Id', 'Name', 'Email'];
$stream->writeCsv($header);
$collection = $this->customerFactory->create()->getCollection();
foreach ($collection as $customer) {
$data = [];
$data[] = $customer->getId();
$data[] = $customer->getName();
$data[] = $customer->getEmail();
$stream->writeCsv($data);
}
}
}