title: Making PHP file upload secure with htaccess
body: <p>I am working on a task to make the PHP file upload secure and my client wants to follow the guideline mention on the website <a href="http://www.acunetix.com/websitesecurity/upload-forms-threat.htm" rel="noreferrer">http://www.acunetix.com/websitesecurity/upload-forms-threat.htm</a></p>
  
  <p>We are able to follow all the guidelines mentioned on this site accept from htaccess rule. They have mention to do following.</p>
  
  <pre><code>Define a .htaccess file that will only allow access to files with allowed extensions.
  Do not place the .htaccess file in the same directory where the uploaded files will be stored. It should be placed in the parent directory.
  A typical .htaccess which allows only gif, jpg, jpeg and png files should include the following (adapt it for your own need). This will also prevent double extension attacks.
  
  deny from all
  &lt;Files ~ "^\w+\.(gif|jpe?g|png)$"&gt;
  order deny,allow
  allow from all
  &lt;/Files&gt;
  If possible, upload the files in a directory outside the server root.
  Prevent overwriting of existing files (to prevent the .htaccess overwrite attack).
  Create a list of accepted mime-types (map extensions from these mime types).
  Generate a random file name and add the previously generated extension.
  Don’t rely on client-side validation only, since it is not enough. Ideally one should have both server-side and client-side validation implemented.
  </code></pre>
  
  <p>So that no other files than gif, jpg and png will be executed. But they don't want the htaccess to be in the folder where we upload images as that folder has permissions and htaccess can be overwritten. So they tell to keep the file at root level.</p>
  
  <p>I want to know that if we keep the file at root level and allow only image types to execute then how my php scripts will be executed? When I add this htaccess at root level, off course my php scripts don't work and return permission error. Struggling to get this work out.</p>
  
  <p>Can you help me with get this working or any other effective way to do this security check. We don't want to keep security loopholes on system.</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Thank you all.</p>