php - Allow users to download files outside webroot -


hello using php allow users upload files , have them sitting in folder outside webroot (/var/www) folder security reasons. in folder /var/uploads. user uploads files specific records. once the uploaded files moved uploads folder, address of attachment stored in database. whenever user checks record, attachments specific record going displayed downloads.

since out of webroot, unable them downloaded have url of

http://localhost/var/uploads/attachment.txt

do have solution or should downloadable folders child directories of webroot?

<?php $con = mysql_connect("localhost","id","pass"); if (!$con)   {   die('could not connect: ' . mysql_error());   }  mysql_select_db("db", $con);  $result = mysql_query("select * attachments");  while($row = mysql_fetch_array($result)) { echo '<a href="'.$row[2].'" target="_blank">download</a>--'.$row[3].'<br>'; }  mysql_close($con); ?>  

is code using. folder's owner www-data:/ or web server. there should no access issues.

use

  • a symlink pointing /var/uploads (tutorial here)

  • a apache alias directive alias /uploads /var/uploads (must in httpd.conf)

  • or proxy php script accepts variable filename=upload.jpg , fetches file e.g. using fpassthru()

the latter least preferable option because resource intensive, it's alternative. needs proper securing prevent attacker getting other files on server through proxy.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -