How To

How to Create a Directory Programmatically in Magento 2?

Hello Magento Friends,

In today’s blog, I will explain How to Create a Directory Programmatically in Magento 2.

Directories in Magento 2 serve as a structured storage mechanism for various files, modules, customizations, caching, and logging. They play a crucial role in organizing, managing, and optimizing the functionality and performance of a Magento 2 store.

So let’s learn how to programmatically create a directory in Magento 2.

Steps to Create a Directory Programmatically in Magento 2:

Step 1: First, create a CreateDirectory.php file inside the etc folder.

app\code\Vendor\Extension\Helper

Then add the below code

<?php
namespace Vendor\Extension\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Directory\WriteInterface;

class CreateDirectory extends AbstractHelper
{ 
    protected $file;

    protected $newCreateDirectory;

    public function __construct(
        Filesystem $file
    ) {
        $this->newCreateDirectory = $file->getDirectoryWrite(DirectoryList::VAR_DIR);
    }

    public function createDirectory()
    {
        $directoryPath = "magecomp";
        $newCreateDirectory = false;
        try
        {
            $newCreateDirectory = $this->newCreateDirectory->create($directoryPath);
        } catch (FileSystemException $e) {
            throw new LocalizedException(
                __("you can't create directory", $directoryPath)
            );
        }
        return $newCreateDirectory;
    }
}

Conclusion:

So you can easily create a directory programmatically in Magento 2. You can even Delete a Directory in Magento 2 Programmatically. If you have any doubt about the above step, ask me through the comment box without any hesitation. Share the article with your friends and stay updated with us.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • Hello

    Please use double quotes for the main string. The line below contains an error:

    __('you can't create directory', $directoryPath)

    Thanks.

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago