Allow users to download files outside webroot(允许用户在 webroot 之外下载文件)
问题描述
Hello I am using PHP to allow users to upload files and I have them sitting in a folder outside webroot (/var/www) folder for security reasons. It is in the folder /var/uploads. A user uploads files for specific records. Once the the uploaded files are moved to the uploads folder, the address of the attachment is stored in the database. Now whenever a user checks the record, attachments for the specific record are going to be displayed for downloads.
Since they are out of the webroot, I am unable to get them downloaded as they would have a url of
http://localhost/var/uploads/attachment.txt
Do we have a solution or should it downloadable folders be child directories of the 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 * from attachments");
while($row = mysql_fetch_array($result))
{
echo '<a href="'.$row[2].'" target="_blank">Download</a>--'.$row[3].'<br>';
}
mysql_close($con);
?>
is the code I am using. The folder's owner is www-data:/ or the web server. So there should be no access issues.
Use
a symlink pointing to
/var/uploads
(tutorial here)a Apache
Alias
directiveAlias /uploads /var/uploads
(must be in httpd.conf)or a proxy PHP script that accepts a GET variable
filename=upload.jpg
and fetches the file e.g. usingfpassthru()
the latter is the least preferable option because it is resource intensive, but sometimes it's the only alternative. It also needs proper securing to prevent an attacker from getting other files on your server through the proxy.
这篇关于允许用户在 webroot 之外下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:允许用户在 webroot 之外下载文件
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01