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
Post a Comment