How to serve documents from outside the web root using PHP?
<p>For security I'm moving a collection of files and folders to outside the web root on an apache server, and then I will serve them dynamically.
This seems better than 2 alternatives:</p>

<ol>
<li>Leave them web accessible and just create a php login page that gets prepended to every file.  The problem is they're not all php files, and I can't prepend a php login file to a pdf, image, etc.</li>
<li>Leave them web accessible and use HTTP authentication to restrict access to the whole directory.  But that introduces problems including cleartext passwords, no graceful logout method, etc.</li>
</ol>

<p>So we're back to having them outside the web root but serving them dynamically.  The problem I'm having is, since they're all different file types (php scripts, txt, pdf, jpg) I'm not sure whether I should use <code>include()</code> or <code>readfile()</code>.  And I run into problems with sending the proper headers for every file so that the browser displays them correctly.</p>

<p>Am I missing another magic solution?  Is there a framework that has eluded me that handles the serving of dynamic files and headers?</p>

<p>(FYI I'm running Linux, Apache &amp; PHP on a shared host)</p>
