---
title: "Download Files in Magento 2"
url: "https://dolphinwebsolution.com/how-to-download-files-in-magento-2"
date: "2021-07-25T12:43:55+05:30"
modified: "2023-08-28T05:37:36+05:30"
author:
  name: "Mahesh Makwana"
categories:
  - "Magento"
word_count: 374
reading_time: "2 min read"
summary: "There are many times we need to allow users to download files from Magento 2 file system. Here we learn how to Download Files in Magento 2. There are many operations for files in Magento like read,..."
description: "There are many times we need to allow users to download files from Magento 2 file system. Here we learn how to Download Files in Magento 2. There are many op..."
keywords: "Download Files in Magento 2,Download Files from Magento 2, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Add Placeholder Text in Field for Checkout in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-add-placeholder-text-in-field-for-checkout-in-magento-2"
  - title: "How to create Inceptor or Plugin in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-create-inceptor-or-plugin-in-magento-2"
  - title: "Add Custom Validation To System Configuration Field In Magento 2"
    url: "https://dolphinwebsolution.com/add-custom-validation-to-system-configuration-field-in-magento-2"
---

# Download Files in Magento 2

_Published: July 25, 2021_  
_Author: Mahesh Makwana_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/blog-image-2-1024x614.jpg)

There are many times we need to allow users to download files from Magento 2 file system. Here we learn how to Download Files in Magento 2. There are many operations for files in Magento like read, write, append, create, delete, insert, update, etc. Below code is performed download any type of file in Magento 2.

```
<?php

namespace Dolphin\SampleDownload\Controller\Index;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\ResultFactory;

class DownloadFile extends \Magento\Framework\App\Action\Action
{
    protected $fileFactory;
    protected $_storeManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
    ) {
        $this->_storeManager = $storeManager;
        $this->fileFactory = $fileFactory;
        parent::__construct($context);
    }
    public function execute()
    {
        $fileName = "test.pdf"; // add your file name here
        if ($fileName) {
            $filePath = 'media/catalog/attachments/file/' . $fileName;
            $content['type'] = 'filename';// type has to be "filename"
            $content['value'] = $filePath; // path where file place
            $content['rm'] = 1; // if you add 1 then it will be delete from server after being download, otherwise add 0.
            $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
            $mediaUrl .= 'catalog/attachments/file/' . $fileName;
            $imageHeaders = get_headers($mediaUrl);
            if (isset($imageHeaders[0])) {
                $imageStatus = stripos($imageHeaders[0], "200 OK") ? true : false;
                if ($imageStatus) {
                    return $this->fileFactory->create($fileName, $content, DirectoryList::PUB);
                } else {
                    $this->messageManager->addError(__("File does not exist"));
                }
            } else {
                $this->messageManager->addError(__("File does not exist"));
            }
        } else {
            $this->messageManager->addError(__("Something went wrong"));
        }
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}
```

In the above code, we add one file to our Magento 2 file system at path <magetno root>/pub/media/catalog/attachments/file/test.pdf. **You can also change the file path and file name from the above code.** And here we also check file is exists or not by using PHP **get\_headers()** method.

**Note: Please Make sure you must be added the right file name with [extension](https://dolphinwebsolution.com/shop/magento-2-extensions.html) and file path where the file exists.**

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.

### You also like to to read this

- [Create a Custom Theme in Magento 2](https://dolphinwebsolution.com/how-to-create-a-custom-theme-in-magento-2)
- [Add Custom Validation Checkout](https://dolphinwebsolution.com/magento-2-add-custom-validation-checkout)


---

_View the original post at: [https://dolphinwebsolution.com/how-to-download-files-in-magento-2](https://dolphinwebsolution.com/how-to-download-files-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 07:39:10 UTC_  
