Site icon MageComp Blog

Solved: Magento 2 Export Product CSV File Permission Issue

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!

Exit mobile version