title: Protect file from direct access
body: <p>I have a folder in which I save files. These files can be accessed from the web page. Let's say that the directory is like <code>manual/fileName.pdf</code>. </p>
  
  <p>I also have a page to show a listing of stored files in the <code>manual/</code> folder and this page is protected using user sessions. So you have to login first before accessing the page. </p>
  
  <p>I read that by placing a <code>.htaccess</code> in the folder, I can prevent users from accessing the files directly. But now I can't access them even from that listing page. </p>
  
  <p>Here what I write in <code>.htaccess</code>:</p>
  
  <pre><code>RewriteEngine on
  RewriteRule .documentation.php
  </code></pre>
  
  <p>And here's my file listing script:</p>
  
  <pre><code>if ($handle = opendir('manual')) {
     while (false !== ($entry = readdir($handle))) {
       if ($entry != "." &amp;&amp; $entry != "..") {
          if (!preg_match('/.php/', $entry) &amp;&amp; !preg_match('/.htaccess/', $entry)) //prevent some file to be shown
              {echo "&lt;a href='manual/$entry' target='_blank'&gt;$entry&lt;/a&gt;&lt;hr&gt;";}
           }
       }
       closedir($handle);
  }
  </code></pre>
  
  <p>Any idea how to access the files?</p>