Note that there are some explanatory texts on larger screens.

plurals
  1. POSelective file download permission with Apache and PHP
    text
    copied!<p>I have a website with a file download option. However not everyone is allowed to download every file. For example if I log in as administrator I can download all files, but if I log in as a customer I can only download certain files.</p> <p>Currently all these files are stored in a directory outside of the web root, so users can't download them manually. I made a PHP script to manage the downloading of files, but it's very buggy. I tried several things:</p> <p>1:</p> <pre><code>echo file_get_contents($file); </code></pre> <p>2:</p> <pre><code>readfile($file); </code></pre> <p>3:</p> <pre><code>if (($handle = fopen($file, "rb")) !== false) { while (!feof($handle)) { echo fread($handle, 4096); } fclose($handle); } </code></pre> <p>I found that all of these methods are very unreliable when testing with a 500MB file. The first and second both fail with all browsers and random percentages. Sometimes they already fail after a few megabytes, while other times they make it as far as 80%. But eventually they'll just stop downloading without any errors.</p> <p>The 3rd method is most reliable. It downloads well in Firefox (albeit way too slow; about 700KB/s while it's from localhost and the readfile-method goes at 25MB/s when it works) and Chrome but in Internet Explorer it mostly fails. In IE it first gets stuck on "getting file information" without getting any "Open/Save" dialog. When I cancel that and try again I get an "Open/Save" dialog.</p> <p>I searched the internet and I can't find any satisfying method that actually works well. Preferably I just want Apache to handle the file download since that always works perfectly without problems. But I don't want to put all files into a public directory since then everyone could download all files.</p> <p>What are my options? Is there a best method?</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