Back to Top

How to add directory using setup script in Magento2

Updated 22 March 2024

How to add directory using setup script in Magento2 :- Here, I am going to show that how to create directory using setup-patch file in magento2.

  • Create AddDirectory.php at this path “Namespace\ModuleName\Setup\Patch\Data”
<?php

namespace Namespace\ModuleName\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Filesystem\Io\File;
use Magento\Framework\App\Filesystem\DirectoryList;

class AddDirectory implements DataPatchInterface
{
    /**
     * @var File
     */
    private $io;

    /**
     * @var DirectoryList
     */
    private $directoryList;

    /**
     * @param File $io
     * @param DirectoryList $directoryList
     */
    public function __construct(
        File $io,
        DirectoryList $directoryList
    ) {
        $this->_io = $io;
        $this->_directoryList = $directoryList;
    }

    public function apply()
    {
        $this->_io->mkdir($this->_directoryList->getPath('media').'/tempFolder', 0755);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }
}

After completing it, When you run upgrade command. This directory will be created at given path.

Thank’s , Hope so it will help you.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home