---
title: Creating Download Link to Direct File in Magento
description: Here is what you need to do for creating a download link to a direct file in Magento
---

[Software Development Blog | Daffodil Software](https://insights.daffodilsw.com/blog)

# [Creating Download Link to Direct File in Magento](https://insights.daffodilsw.com/blog/creating-download-link-to-direct-file-in-magento)

 Written by [Ankita Agrawal](https://insights.daffodilsw.com/blog/author/ankita-agrawal) | Jun 11, 2015 5:38:45 PM

One of the most beautiful parts of Magento lies in its usage. From the admin control you can basically oversee and spearhead any feature you wish to utilize. Though proper implementation and feasibility may seem difficult or impossible at times but even these intricate features can be utilized very adeptly.

<https://insights.daffodilsw.com/hubfs/Imported_Blog_Media/magento1.png>

One such feature that I implemented was for a client who wanted to provide a direct download link of a file stored in a “var” folder.

Basically I have a custom admin module and client want to provide a link in grid that will directly download the file. File may be an image or a pdf. File location is within var folder and as it is known we cannot access var folder directly so we need to create admin controller to achieve this functionality.

First we need to call render function in grid.php file.

Path of file is – app/code/local/ModuleNamespace/ModuleName/Block/Adminhtml/ModuleName/Grid.php

 \[php\]$this->addColumn("column\_name", array(

"header" => Mage::helper("registration")->\_\_("Column Name"),  
 "index" => "column\_name",  
 'sortable' => true,  
 'type' => 'text',  
 'renderer' => 'ModuleNamespace\_ModuleName\_Block\_Adminhtml\_Template\_Grid\_Renderer\_File' )); \[/php\]

This will call render function of file.php and pass path of the file that we need to download.

Path of file should be –  app/code/local/ModuleNamespace/ModuleName/Block/Adminhtml/Template/Grid/Renderer/File.php

Now file.php should be like this-

\[php\]<?php class ModuleNamespace\_ModuleName\_Block\_Adminhtml\_Template\_Grid\_Renderer\_File extends Mage\_Adminhtml\_Block\_Widget\_Grid\_Column\_Renderer\_Abstract {

public function render(Varien\_Object $row) {  
 return $this->\_getValue($row);  
 }

protected function \_getValue(Varien\_Object $row) {

$val = $row->getData($this->getColumn()->getIndex());  
 $parts = explode("/", $val);  
 $filename = end($parts);  
 $url = Mage::helper("adminhtml")->getUrl("moduleName/adminhtml\_moduleName/downloadfile", array('filename' => $filename)); $out = "<a href='".$url."'>". $val ."</a>";  
 return $out;  
 }  
 }\[/php\]

here $val contain the path of file which should be like

“var/www/projectName/var/folderName/file.jpg”

$filename will get the file name like “file.jpg”. If you already have the file name then there is no need to explode it. Now I am cal

[View full post](https://insights.daffodilsw.com/blog/creating-download-link-to-direct-file-in-magento)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Ankita Agrawal"
  },
  "dateModified" : "2017-09-21T12:15:22.044Z",
  "datePublished" : "2015-06-11T17:38:45Z",
  "headline" : "Creating Download Link to Direct File in Magento",
  "image" : {
    "@type" : "ImageObject",
    "height" : 281,
    "url" : "http://appdevelopment.daffodilsw.com/hubfs/Imported_Blog_Media/magento1.png",
    "width" : 544
  },
  "mainEntityOfPage" : "https://insights.daffodilsw.com/blog/creating-download-link-to-direct-file-in-magento",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60.0,
      "url" : "https://insights.daffodilsw.com/hubfs/daffodil-orignal-2.png",
      "width" : 199.60396
    },
    "name" : "Curated Engineering Insights"
  }
}
```