How To

Solved: Magento 2 Export Product CSV File Permission Issue

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

<?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

<?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.

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!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

22 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

22 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago