I need to force a csv file's download from a cms page in Magento. I've looking for many forums about it and I created a custom php code and insert it in blank empty cms page in Magento but the downloaded csv file contains html code with scripts and html tags before the file info.

So, how could I download the file without html code and only with the info I need?

  • I guess you need to set the right headers for this page and attach your csv. But I'm worried it won't come out right on a cms page, I'd advise you to use a link to a custom php, where you'd be able to correctly set the headers and echo your csv then die properly. Feb 12 '15 at 10:31
  • Hello. In cms page I only added this code {{block type="core/template" template="customphp/files.phtml"}} with empty design. And in "files.phtml" I put php code to force a file download. I don't understand if page has not design, why html code appears in csv file. Any suggestions? Thanks a lot.
    – Txispas
    Feb 12 '15 at 10:59
  • Here is the code in files.phtml:
    – Txispas
    Feb 12 '15 at 12:55
  • $file = path-to-file header('Content-Type: application/download'); header('Content-Disposition: attachment; filename="catalogo.csv"'); header("Content-Length: " . filesize("catalogo.csv")); $fp = fopen($file, "r"); fpassthru($fp); fclose($fp);
    – Txispas
    Feb 12 '15 at 12:56
  • it's a cms page, no matter what you put in your phtml, header footer and such will still be loaded. Feb 13 '15 at 12:46

Just upload the CSV anywhere inside media/ and create a link in the HTML page pointing to the CSV file (e.g. http://www.yourstore.com/media/test.csv). The browser will allow you to open the file in an office application or download the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.