5

I am trying to create a new directory in /pub/media/import folder. Creating a directory works with mkdir() but I'm having issues with folder persmission rights.

How would I create a directory with Magento\Framework\Filesystem?

My current code:

private function createImagesDir()
{
    if ( ! file_exists($this->directoryList->getPath('media').'/import/images')) {
        mkdir($this->directoryList->getPath('media').'/import/images','775', true);
    }
}
13

you can use \Magento\Framework\Filesystem\Io\File.
Either inject an instance of it in your class or instantiate it manually using new.
Then do this:

/** @var \Magento\Framework\Filesystem\Io\File $io **/
$io->mkdir($this->directoryList->getPath('media').'/import/images', 0775);

Also, the problem might be because your rights say '775' as string. Try changing it to 0775.

0

Sometimes less is more.

The simplest option is to not specify the file permissions.

Leave it to the webserver to create a folder it can read using the default umask.

For example:

private $file;
private $dir;



public function __construct(
    ...
    \Magento\Framework\Filesystem\Io\File $file,
    \Magento\Framework\Filesystem\DirectoryList $dir,
    ...
) {
    $this->file = $file;
    $this->dir = $dir;
    ...
}



private function getImageImportFolder()
{
    $images = $this->dir->getPath('media').'/import/images';
    if ( ! file_exists($images)) {
        $this->file->mkdir($images);
    }
    return $images . '/';
}
0

Check Magento\Framework\Filesystem\Io\File , there already is a specific method checkAndCreateFolder .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.