0

I want to create media directory with 777 recursive permission on custom module installation time, So I have created createDirectory function in InstallSchema.
But currently directory is created with drwxrwxr-x permission. so i want to give 777 permission.

Here is my InstallSchema.php.

<?php

/**
 * VendorNameSpace ModuleName
 * Class Description
 */

namespace VendorNameSpace\ModuleName\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

class InstallSchema implements InstallSchemaInterface {

    /**
     * Blog Post Module's Media Directories
     */
    const MEDIA_TMP_PATH = 'vendornamespace/modulename/tmp/upload';
    const MEDIA_PATH = 'vendornamespace/modulename/upload';

    /**
     * @var Magento\Framework\App\Filesystem\DirectoryList
     */
    private $_directoryList;

    /**
     * @var Magento\Framework\Filesystem\Io\File 
     */
    private $_ioFile;


    /**
     * @param \Magento\Framework\Filesystem\Io\File $ioFile
     * @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList
     */
    public function __construct(
        \Magento\Framework\Filesystem\Io\File $ioFile,
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList
    ) {
        $this->_ioFile = $ioFile;
        $this->_directoryList = $directoryList;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(
        SchemaSetupInterface $setup, 
        ModuleContextInterface $context
    ) {

        $this->_createMediaDir();


        /**
         * Database Script
         *
         *
         *  ......
         */

    }

    /**
     * Creating Media Directories
     */
    protected function _createMediaDir() {

        $mediaTemp = $this->_directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . SELF::MEDIA_TMP_PATH;
        $media = $this->_directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . SELF::MEDIA_PATH;

        $directories = [$media, $mediaTemp];
        $ioAdapter = $this->_ioFile;

        foreach ($directories as $dir) {
            if(!is_dir($dir)){
                $ioAdapter->mkdir($dir,0777);
            }
        }

    }
}
  • I found the solution by giving parent directory to recursive permission. – Himanshu Sep 27 '18 at 10:39
0

I have found the exact way to make it accessible for everyone you have to give 0777 permission after creating.

You have to additionally add chmodRecursive of \Magento\Framework\Filesystem\Io\File function to give parent directory 0777.

protected function _createMediaDir() {

    $mainMediaDir = "vendornamespace"; // you can put it also as constant as below directory
    $mediaTemp = $this->_directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . SELF::MEDIA_TMP_PATH;
    $media = $this->_directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . SELF::MEDIA_PATH;

    $directories = [$media, $mediaTemp];
    $ioAdapter = $this->_ioFile;

    foreach ($directories as $dir) {
        // Use `checkAndCreateFolder` instead of mkdir so it will check if directory available or not
        $ioAdapter->checkAndCreateFolder($dir);
    }

    // Give recursive permission to main media directory That's it!..
    $ioAdapter::chmodRecursive($mainMediaDir, 0777);
    $ioAdapter->close();

    return true;
}

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.