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

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

7 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago