title: protect files via .htaccess and PHP
body: <p>Might have been discussed before, but I just can't find a quick and clean solution.</p>
  
  <p>1) I have a bunch of files in my <code>/data/</code> folder: .jpg, .gif, .mp4, .pdf, etc.</p>
  
  <p>2) I want to create a <code>/data/.htaccess</code> file that will catch each request and redirect it to a PHP processor, e.g.: </p>
  
  <pre><code>/data/filename.jpg -&gt; /data/processor.php?file=filename.jpg
  </code></pre>
  
  <p>3) the <code>processor.php</code> will do as follows:</p>
  
  <p>3a) Check if you have a valid <code>$_SESSION['logged_in'] == TRUE</code></p>
  
  <p>3b) Send corresponding headers</p>
  
  <p>3c) Send file data</p>
  
  <p>Ideally the .htaccess redirect won't change the browser's URL, so all the current file requests will stay valid AS LONG AS you are logged in.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As it was suggested the better approach would be to keep my files outside of the web root. Thus, I'm going to only leave <code>/html/data/processor.php</code> and move all media files (like .jpg and .mp4) to <code>/media/</code> outside of web root.</p>