Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After researching and trying a lot of test &amp; trials, I found some facts about the <code>header()</code> function and <code>readfile()</code> or similar ones.</p> <ol> <li><p>First of all you should know that <code>header()</code> has compatibility issues with different browsers. So don't try to look for a function that is flawless. The issue isn't the php function, it's the browsers that has different way of analyzing a download header so they all behave differently. And you may know already, the biggest troublemaker is IE</p></li> <li><p>If you want to fix the problem and make it work using <code>header()</code>, You'll have to add/force some header details (<code>Content-Type</code>,etc) , But this is not possible in your php code. No matter how many times you try, it just doesn't work that work. I don't have much info on this, But I just know even if you add the <code>Content-Type</code> headers before the actual <code>header()</code> it won't work. seems they are completely for different purposes.</p></li> <li><p>You can use add the necessary headers and use the <code>readfile()</code> function instead and it will work fine. But you should know that this function will load/read the file/url you're calling into the memory. This could be a big problem and will probably slow down your webserver, The worst situation is that it could crash as well. This can happen if you have too downloaders or many files.</p></li> </ol> <p>Unfortunately, as I mentioned before, you can't fix this in your php code. So what I did in order to fix this problem, was adding the headers in my webserver vhost.</p> <p>NginX:</p> <pre><code>location ~ ^/.*\.mp3$ { add_header Pragma "public"; add_header Expires "0"; add_header Cache-Control "must-revalidate, post-check=0, pre-check=0"; add_header Content-Disposition "attachment"; add_header Content-Type "application/force-download"; add_header Content-Type "application/octet-stream"; add_header Content-Type "application/download"; add_header Content-Transfer-Encoding "binary"; } </code></pre> <p>In my case, the files users had to download was in mp3 format. But you can change it or set a global directive so it can work with any format.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload