ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Downloading files instead of displaying them in the browser

May 31, 2013 2:30pm

Subscribe [3]
  • #1 / May 31, 2013 2:30pm

    thejame

    24 posts

    I know this can be done using PHP but because ExpressionEngine has its own way of doing things it’s not working as expected.  That or I am not doing it correctly.
    I just want to be able to force downloads of certain files (mp3, pdf, etc) instead of having them play in the browser.

    I’m passing a link like this:

    {exp:channel:entries ... }
    <a href="http://.../download?file={my_file_url}">Download audio</a>
    {/exp:channel:entries}

    And in the “download” template with PHP enabled I have:

    <?php
      $file = $_REQUEST["file"];
      header('Content-Type: application/octet-stream');
      header('Content-Length: '.filesize($file));
      header('Content-Description: File Transfer');  
      header('Content-Disposition: attachment; filename="download.mp3"');
      readfile($file);
    ?>

    When run I get a file with the entire URL path (replacing the slashes with dashes) containing only this:
    Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 18158324 bytes) in public_html/dev/system/expressionengine/libraries/Functions.php(679) : eval()‘d code on line 1

    Any ideas?  Thanks…

  • #2 / Jun 04, 2013 5:02am

    Simply add the following to the end of your .htaccess file and remove all that php code in your templates.

    <FilesMatch "\.(?i:mp3)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    </FilesMatch>
    <FilesMatch "\.(?i:pdf)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    </FilesMatch>

    Stefan

  • #3 / Jun 04, 2013 8:56am

    chequan

    18 posts

    But, if I move upload folder to out of wwwroot, it will be invisible for http request directly. It mush use PHP to read and output this file content.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases