How to Create a Directory Programmatically in Magento 2?

 <?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;

    }

}

Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Set and Get Cookie in Magento 2

How to Get Invoice Comments List Programmatically in Magento 2