Reading list Switch to dark mode

    How to copy module files to other directory in Magento 2

    Updated 30 March 2023

    Sometimes we need to get access of the files on the storefront so we need to copy the required files in pub directory. Today we will learn how to copy module’s files such as images, text etc to media directory. You can use the following code in Helper, Controller or Patch etc.

    We will follow the following module https://webkul.com/blog/create-hello-module-in-magento2/ for the reference.

    Suppose we have image in our following directory

    /var/www/html/Magento/app/code/Webkul/Hello/view/base/web/images/marketplace/banner/noimage.png

    And we want to copy this file to the following directory

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    /var/www/html/Magento/pub/media/marketplace/banner/noimage.png

    <?php
    /**
     * Webkul Software
     *
     * @category  Webkul
     * @package   Webkul_Hello
     * @author    Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    namespace Webkul\Hello\Controller\Index;
    
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\App\Filesystem\DirectoryList;
    
    class CopyFile extends Action
    {
        /**
         * @var \Magento\Framework\Module\Dir\Reader
         */
        private $reader;
        /**
         * @var \Magento\Framework\Filesystem
         */
        private $filesystem;
        /**
         * @var \Magento\Framework\Filesystem\Io\File
         */
        private $file;
        /**
         * @param \Magento\Framework\Filesystem $filesystem
         * @param \Magento\Framework\Filesystem\Io\File $file
         * @param \Magento\Framework\Module\Dir\Reader $reader
         * @param Context $context
         */
        public function __construct(
            \Magento\Framework\Filesystem $filesystem,
            \Magento\Framework\Filesystem\Io\File $file,
            \Magento\Framework\Module\Dir\Reader $reader,
            Context $context
        ) {
            $this->filesystem = $filesystem;
            $this->file = $file;
            $this->reader = $reader;
            parent::__construct($context);
        }
    
        /**
         * Execute function for validate the Otp from Customers
         *
         * @param none
         * @return mixed
         */
        public function execute()
        {
            try {
                $this->createDirectories();
                $directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
                $ds = "/";
                $baseModulePath = $this->reader->getModuleDir('', 'Webkul_Hello');
                $mediaDetails = [
                    "marketplace/banner" => [
                        "view/base/web/images/marketplace/banner" => [
                            "noimage.png"
                        ]
                    ]
                ];
    
                foreach ($mediaDetails as $mediaDirectory => $imageDetails) {
                    foreach ($imageDetails as $modulePath => $images) {
                        foreach ($images as $image) {
                            $path = $directory->getAbsolutePath($mediaDirectory);
                            $mediaFilePath = $path.$ds.$image;
                            $moduleFilePath = $baseModulePath.$ds.$modulePath.$ds.$image;
                            if ($this->file->fileExists($mediaFilePath)) {
                                continue;
                            }
                            if (!$this->file->fileExists($moduleFilePath)) {
                                continue;
                            }
                            $this->file->cp($moduleFilePath, $mediaFilePath);
                        }
                    }
                }
                echo "Files have been copied.";
            } catch (\Exception $e) {
                $error = true;
            }
        }
        /**
         * Create default directories
         */
        private function createDirectories()
        {
            $mediaDirectories = ['marketplace', 'marketplace/banner'];
            foreach ($mediaDirectories as $mediaDirectory) {
                $directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
                $path = $directory->getAbsolutePath($mediaDirectory);
                if (!$this->file->fileExists($path)) {
                    $this->file->mkdir($path, 0777, true);
                }
            }
        }
    
    }

    Thanks 🙂

    . . .

    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