Magento – Unable to make a direct download link of downloadable product. Links return 404

downloaddownloadablemagento-1.9product-list

I modified my list.php to get the download link button instead of "add to cart" button when I have a free download file.
I know that in this way I can't get statistic of download but for now this is not a problem.

Here my code:

<?php if(!($_product->getIsVirtual() && $_product->getFinalPrice() == 0)): ?>
                        <p class="action"><button type="button" title="<?php echo $this->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>

                            <?php else:
                            $_myprodlinks = Mage::getModel('downloadable/link');
                            $_myLinksCollection = $_myprodlinks->getCollection()->addProductToFilter($_product->getId());
                            if (sizeof($_myLinksCollection)>0):
                            foreach ($_myLinksCollection as $_link):
                            $mediaUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
                            $_linkpath = $mediaUrl."downloadable/files/links".$_link->getLinkFile();
                            ?>
                                <a href="<?php echo $_linkpath ?>" target="_blank" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('downloadable')->__('Start Download')) ?>"><button type="button" title="<?php echo Mage::helper('core')->quoteEscape(Mage::helper('downloadable')->__('Start Download')) ?>" class="button btn-cart"><span><span><?php echo Mage::helper('core')->quoteEscape(Mage::helper('downloadable')->__('Start Download')) ?></span></span></button></a>
                            <?php
                             endforeach;
                             endif;?>       
                        <?php endif;?>

This works fine! I have the button and the link has the right url like this

http://my-site.com/media/downloadable/files/links/a/u/auguri.pdf

Through FTP I have verified the presence of the file, but this link return me a 404 page.
I have read some similar topics and I tried to change the permission of folders and files, but nothing change.
Any suggestion?

Best Answer

I know this does not solve your problem, but maybe it makes you rework the functionality:

Actually the correct behaviour when accessing files directly from /media/downloadable/ should be a Permission denied.

Why?

Anyone could just do a directory traversal in your /media or /media/downloadable directory and download all the files in there automatically.

If you don't care about this fact: go ahead.

If you do: Try to find another solution to download the files.

You can use the Sample section/function of the downloadable products which is intended to be used to preview the files. Just upload your file there.

Related Topic