php – 如何下载访问public_html目录之外的文件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何下载访问public_html目录之外的文件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2304字,纯文字阅读大概需要4分钟。
内容图文
![php – 如何下载访问public_html目录之外的文件?](/upload/InfoBanner/zyjiaocheng/752/25df01aff6b6485591c9d3692ad6f3cb.jpg)
出于安全考虑,我将文件存储在public_html文件夹之外.但是,我想以某种方式链接到特定文件,用户可以下载其中一个文件.
我正在使用一个jquery脚本,它允许我将服务器PATH指定为上传文件夹,并且它会在public_html文件夹之外上传.
唯一的问题是它要求我指定用于下载文件的“上传路径”的URL.我想我可能会喜欢:
public_html/redirect (contains htaccess which forwards all requests to "hiding" folder)
hiding (outside public_html)
A user clicks /redirect/file.doc and they download a file located at hiding/file.doc
这可能吗?如果没有,我如何给我的public_html目录之外的文件提供特定的文件下载访问权限?我知道我之前已经看过它在其他脚本上完成了……
解决方法:
您可以使用“php download handler”执行此操作:
您可以使用这样的方法将文件内容和文件信息标题返回给用户浏览器,只需确保在此之前没有输出任何其他内容.
我建议你把它放在单独的文件中并调用它,例如download.php.
function returnFile( $filename ) {
// Check if file exists, if it is not here return false:
if ( !file_exists( $filename )) return false;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
// Suggest better filename for browser to use when saving file:
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
// Caching headers:
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// This should be set:
header('Content-Length: ' . filesize($filename));
// Clean output buffer without sending it, alternatively you can do ob_end_clean(); to also turn off buffering.
ob_clean();
// And flush buffers, don't know actually why but php manual seems recommending it:
flush();
// Read file and output it's contents:
readfile( $filename );
// You need to exit after that or at least make sure that anything other is not echoed out:
exit;
}
将其扩展为基本用途:
// Added to download.php
if (isset($_GET['file'])) {
$filename = '/home/username/public_files/'.$_GET['file'];
returnFile( $filename );
}
警告:
这是一个基本的例子,并没有考虑到用户可能会尝试利用未正确消毒的$_GET的一些邪恶优势.
这意味着,如果某些条件适用,用户可以例如检索passwd文件或其他一些敏感信息.
例如,检索/ etc / passwd:
只需将浏览器指向http://server.com/download.php?file=../../../etc/passwd,服务器就会返回该文件.因此,在实际使用之前,您应该了解如何正确检查和清理任何用户提供的参数.
内容总结
以上是互联网集市为您收集整理的php – 如何下载访问public_html目录之外的文件?全部内容,希望文章能够帮你解决php – 如何下载访问public_html目录之外的文件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。