gpt4 book ai didi

php - 如何阻止直接下载文件

转载 作者:行者123 更新时间:2023-12-01 22:21:50 25 4
gpt4 key购买 nike

我的网站包含一些下载内容。我只想为登录用户访问下载此文件。

如果用户在浏览器中直接输入文件 url,如果用户未登录,则会显示禁止访问的页面。我没有使用任何 CMS。直接文件链接:localhost/files/questions/20160917070900-w2CdE9LZpE.zip

我在网上搜索但没有找到任何好的答案。请建议我该怎么做。

最佳答案

进入文件夹 members 创建新的文件夹文件,将所有歌曲移至此处,创建新的 .htaccess 文件并添加以下行:

Order Deny,Allow
Deny from all

在members文件夹中创建文件get_file.php并添加如下代码:

if( !empty( $_GET['name'] ) )
{
// check if user is logged
if( is_logged() )
{
$file_name = preg_replace( '#[^-\w]#', '', $_GET['name'] );
$question_file = "{$_SERVER['DOCUMENT_ROOT']}/files/questions/{$file_name}.zip";
if( file_exists( $question_file ) )
{
header( 'Cache-Control: public' );
header( 'Content-Description: File Transfer' );
header( "Content-Disposition: attachment; filename={$question_file}" );
header( 'Content-Type: application/zip' );
header( 'Content-Transfer-Encoding: binary' );
readfile( $question_file );
exit;
}
}
}
die( "ERROR: invalid song or you don't have permissions to download it." );

获取文件的URL:localhost/get_file.php?name=file_name

关于php - 如何阻止直接下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39550660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com