{"id":374776,"date":"2023-03-30T09:16:23","date_gmt":"2023-03-30T09:16:23","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=374776"},"modified":"2023-03-30T09:16:32","modified_gmt":"2023-03-30T09:16:32","slug":"how-to-copy-module-files-to-other-directory-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/","title":{"rendered":"How to copy module files to other directory in Magento 2"},"content":{"rendered":"\n<p>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&#8217;s files such as images, text etc to media directory. You can use the following code in Helper, Controller or Patch etc.<\/p>\n\n\n\n<p>We will follow the following module <a href=\"https:\/\/webkul.com\/blog\/create-hello-module-in-magento2\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/webkul.com\/blog\/create-hello-module-in-magento2\/<\/a>  for the reference.<\/p>\n\n\n\n<p>Suppose we have image in our following directory<\/p>\n\n\n\n<p>\/var\/www\/html\/Magento\/app\/code\/Webkul\/Hello\/view\/base\/web\/images\/marketplace\/banner\/noimage.png<\/p>\n\n\n\n<p>And we want to copy this file to the following directory<\/p>\n\n\n\n<p>\/var\/www\/html\/Magento\/pub\/media\/marketplace\/banner\/noimage.png<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software\n *\n * @category  Webkul\n * @package   Webkul_Hello\n * @author    Webkul\n * @copyright Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\n\nnamespace Webkul\\Hello\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\App\\Filesystem\\DirectoryList;\n\nclass CopyFile extends Action\n{\n    \/**\n     * @var \\Magento\\Framework\\Module\\Dir\\Reader\n     *\/\n    private $reader;\n    \/**\n     * @var \\Magento\\Framework\\Filesystem\n     *\/\n    private $filesystem;\n    \/**\n     * @var \\Magento\\Framework\\Filesystem\\Io\\File\n     *\/\n    private $file;\n    \/**\n     * @param \\Magento\\Framework\\Filesystem $filesystem\n     * @param \\Magento\\Framework\\Filesystem\\Io\\File $file\n     * @param \\Magento\\Framework\\Module\\Dir\\Reader $reader\n     * @param Context $context\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\Filesystem $filesystem,\n        \\Magento\\Framework\\Filesystem\\Io\\File $file,\n        \\Magento\\Framework\\Module\\Dir\\Reader $reader,\n        Context $context\n    ) {\n        $this-&gt;filesystem = $filesystem;\n        $this-&gt;file = $file;\n        $this-&gt;reader = $reader;\n        parent::__construct($context);\n    }\n\n    \/**\n     * Execute function for validate the Otp from Customers\n     *\n     * @param none\n     * @return mixed\n     *\/\n    public function execute()\n    {\n        try {\n            $this-&gt;createDirectories();\n            $directory = $this-&gt;filesystem-&gt;getDirectoryRead(DirectoryList::MEDIA);\n            $ds = &quot;\/&quot;;\n            $baseModulePath = $this-&gt;reader-&gt;getModuleDir(&#039;&#039;, &#039;Webkul_Hello&#039;);\n            $mediaDetails = &#091;\n                &quot;marketplace\/banner&quot; =&gt; &#091;\n                    &quot;view\/base\/web\/images\/marketplace\/banner&quot; =&gt; &#091;\n                        &quot;noimage.png&quot;\n                    ]\n                ]\n            ];\n\n            foreach ($mediaDetails as $mediaDirectory =&gt; $imageDetails) {\n                foreach ($imageDetails as $modulePath =&gt; $images) {\n                    foreach ($images as $image) {\n                        $path = $directory-&gt;getAbsolutePath($mediaDirectory);\n                        $mediaFilePath = $path.$ds.$image;\n                        $moduleFilePath = $baseModulePath.$ds.$modulePath.$ds.$image;\n                        if ($this-&gt;file-&gt;fileExists($mediaFilePath)) {\n                            continue;\n                        }\n                        if (!$this-&gt;file-&gt;fileExists($moduleFilePath)) {\n                            continue;\n                        }\n                        $this-&gt;file-&gt;cp($moduleFilePath, $mediaFilePath);\n                    }\n                }\n            }\n            echo &quot;Files have been copied.&quot;;\n        } catch (\\Exception $e) {\n            $error = true;\n        }\n    }\n    \/**\n     * Create default directories\n     *\/\n    private function createDirectories()\n    {\n        $mediaDirectories = &#091;&#039;marketplace&#039;, &#039;marketplace\/banner&#039;];\n        foreach ($mediaDirectories as $mediaDirectory) {\n            $directory = $this-&gt;filesystem-&gt;getDirectoryRead(DirectoryList::MEDIA);\n            $path = $directory-&gt;getAbsolutePath($mediaDirectory);\n            if (!$this-&gt;file-&gt;fileExists($path)) {\n                $this-&gt;file-&gt;mkdir($path, 0777, true);\n            }\n        }\n    }\n\n}<\/pre>\n\n\n\n<p>Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 <a href=\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":440,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121,41,174,1],"tags":[13901,13902,2460],"class_list":["post-374776","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-module","category-plugin-2","category-uncategorized","tag-copy-file","tag-file-directory","tag-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to copy module files to other directory in Magento 2 - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to copy module files to other directory in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"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&#8217;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 [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-30T09:16:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-30T09:16:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Amir Khan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amir Khan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\"},\"author\":{\"name\":\"Amir Khan\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f2493d3639b5e8c05ae4e9af5f71063a\"},\"headline\":\"How to copy module files to other directory in Magento 2\",\"datePublished\":\"2023-03-30T09:16:23+00:00\",\"dateModified\":\"2023-03-30T09:16:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\"},\"wordCount\":122,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Copy File\",\"File Directory\",\"Magento 2\"],\"articleSection\":[\"Magento 2\",\"module\",\"Plugin\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\",\"name\":\"How to copy module files to other directory in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2023-03-30T09:16:23+00:00\",\"dateModified\":\"2023-03-30T09:16:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to copy module files to other directory in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f2493d3639b5e8c05ae4e9af5f71063a\",\"name\":\"Amir Khan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/da678755fca2993231591a761683dc95fcf81f335982ea5c1b38e9ccc1c6bc0c?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/da678755fca2993231591a761683dc95fcf81f335982ea5c1b38e9ccc1c6bc0c?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Amir Khan\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/amir-khan754\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to copy module files to other directory in Magento 2 - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"How to copy module files to other directory in Magento 2 - Webkul Blog","og_description":"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&#8217;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 [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2023-03-30T09:16:23+00:00","article_modified_time":"2023-03-30T09:16:32+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Amir Khan","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Amir Khan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/"},"author":{"name":"Amir Khan","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f2493d3639b5e8c05ae4e9af5f71063a"},"headline":"How to copy module files to other directory in Magento 2","datePublished":"2023-03-30T09:16:23+00:00","dateModified":"2023-03-30T09:16:32+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/"},"wordCount":122,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Copy File","File Directory","Magento 2"],"articleSection":["Magento 2","module","Plugin"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/","name":"How to copy module files to other directory in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2023-03-30T09:16:23+00:00","dateModified":"2023-03-30T09:16:32+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-copy-module-files-to-other-directory-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to copy module files to other directory in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f2493d3639b5e8c05ae4e9af5f71063a","name":"Amir Khan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/da678755fca2993231591a761683dc95fcf81f335982ea5c1b38e9ccc1c6bc0c?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/da678755fca2993231591a761683dc95fcf81f335982ea5c1b38e9ccc1c6bc0c?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Amir Khan"},"url":"https:\/\/webkul.com\/blog\/author\/amir-khan754\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/374776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/440"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=374776"}],"version-history":[{"count":2,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/374776\/revisions"}],"predecessor-version":[{"id":374781,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/374776\/revisions\/374781"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=374776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=374776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=374776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}