我的网站包含一些下载内容。我只想为登录用户访问下载此文件。
如果用户在浏览器中输入直接文件 url,如果用户未登录,它会显示禁止页面。我没有使用任何 CMS。直接文件链接:localhost/files/questions/20160917070900-w2CdE9LZpE.zip
我在网上搜索,但没有找到任何好的答案。请建议我该怎么做。
进入文件夹成员创建新文件夹文件,将所有歌曲移到此处,创建新的 .htaccess 文件并添加以下行:
Order Deny,Allow
Deny from all
Run Code Online (Sandbox Code Playgroud)
在文件夹成员中创建文件 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." );
Run Code Online (Sandbox Code Playgroud)
获取文件的 URL:localhost/get_file.php?name=file_name