How to write text file and create directory in Magento 2 standard way?

You can write the content dynamically in text(.txt) file using Programmatic way with Magento 2 Standard approach.

Sometimes you need to create a directory and write a text file in the specific location of the system, You can add the content with interface Magento\Framework\Filesystem\Directory\WriteInterface.

I will show you to demo for writing the text file at a var folder with a custom folder. Let’s think our file is stored under the var/custom/demo.txt file in the Magento root. You need to create a Model class with a user-defined function to create a custom directory and demo text file.

This scenario is used when you have integrated Third-party tools for the order import/export and you can synchronization the last order send to the Third-party tools.

You can track the order id, with write down the latest order id to a text file, which was last successfully imported to the Third-party platform.

Leave a Reply