Hello Magento Friends,
In today’s blog, we will learn to solve permission issue while exporting product CSV file in Magento 2.
In Magento 2, you can export and import various types of data using a CSV file from the admin dashboard. Sometimes when you export a product CSV file at that time, you may get the “The path is not writable” error.
Actually, it’s a permission error which says you do not have permission to download the file. To resolve Magento 2 Export Product CSV File Permission Issue, check out the below steps.
Steps to Solve Magento 2 Export Product CSV File Permission Issue:
Step 1: First, you must create a di.xml file in the path below.
app/code/Vendor/Extension/etc/adminhtml/di.xml
And add the following code
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\ImportExport\Controller\Adminhtml\Export\File\Download" type="Vendor\Extension\Controller\Adminhtml\Export\File\Download" /> </config> |
Step 2: After that, you must create a preference file in the following path.
app/code/Vendor/Extension/Controller/Adminhtml/Export/File/Download.php
Then add the below-mentioned code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Vendor\Extension\Controller\Adminhtml\Export\File; use Magento\Backend\App\Action; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\ImportExport\Controller\Adminhtml\Export as ExportController; use Magento\Framework\Filesystem; use Magento\ImportExport\Model\LocalizedFileName; use Throwable; /** * Controller that download file by name. */ class Download extends \Magento\ImportExport\Controller\Adminhtml\Export\File\Download { /** * Url to this controller */ const URL = 'adminhtml/export_file/download/'; /** * @var FileFactory */ private $fileFactory; /** * @var Filesystem */ private $filesystem; /** * @var LocalizedFileName */ private $localizedFileName; /** * DownloadFile constructor. * @param Action\Context $context * @param FileFactory $fileFactory * @param Filesystem $filesystem * @param LocalizedFileName|null $localizedFileName */ public function __construct( Action\Context $context, FileFactory $fileFactory, Filesystem $filesystem, ?LocalizedFileName $localizedFileName = null ) { parent::__construct($context,$fileFactory,$filesystem); $this->fileFactory = $fileFactory; $this->filesystem = $filesystem; $this->localizedFileName = $localizedFileName ?? ObjectManager::getInstance()->get(LocalizedFileName::class); } /** * Controller basic method implementation. * * @return \Magento\Framework\Controller\Result\Redirect | \Magento\Framework\App\ResponseInterface */ public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setPath('adminhtml/export/index'); $fileName = $this->getRequest()->getParam('filename'); $exportDirectory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_IMPORT_EXPORT); try { $fileExist = $exportDirectory->isExist('export/' . $fileName); } catch (Throwable $e) { $fileExist = false; } if (empty($fileName) || !$fileExist) { $this->messageManager->addErrorMessage(__('Please provide valid export file name')); return $resultRedirect; } try { $path = 'export/' . $fileName; $directory = $this->filesystem->getDirectoryRead(DirectoryList::VAR_IMPORT_EXPORT); if ($directory->isFile($path)) { return $this->fileFactory->create( $fileName, $directory->readFile($path), DirectoryList::VAR_DIR, 'application/octet-stream' ); } $this->messageManager->addErrorMessage(__('%1 is not a valid file', $fileName)); } catch (\Exception $exception) { $this->messageManager->addErrorMessage($exception->getMessage()); } return $resultRedirect; } } |
Step 3: After that, run the below commands.
1 2 |
php bin/magento setup:di:compile php bin/magento cache:flush |
Conclusion:
This way, you can quickly resolve permission issue while exporting product CSV file. Alternatively, you can Export Data into a CSV File Programmatically in Magento 2.
If you cannot solve the product export CSV file permission issue, let me know through the comment box and I will quickly provide you with the solution. Share the solution with your friends to help them eliminate the export permission issue. Stay in touch with us for more!
Happy Coding!