I am writing a Magento 2 extension, I would like to know how to create and write files within Magento 2 extension
You can use Magento 2 core class Magento\Framework\Filesystem to handle file operations.
$directory = "/"; $FileName = "test.txt"; $contents = "test content for create and write files programmatically in Magento 2"; $object = \Magento\Framework\App\ObjectManager::getInstance(); $filesystem = $object->get('Magento\Framework\Filesystem'); $writer = $filesystem->getDirectoryWrite($directory); $file = $writer->openFile($FileName, 'w'); try { $file->lock(); try { $file->write($contents); } finally { $file->unlock(); } } finally { $file->close(); }