两种方式,简单2. 借助apache规则或1. 借助插件中的自定义代码。
1。插件
您可以使用
init
钩子和get值
$_GET[ \'file\' ];
. 如果用户具有此get值,请跳入一个函数以检查文件的访问权限:例如,在元框中添加一个复选框。
add_action( \'init\', \'fb_init\' );
function fb_init() {
// this in a function for init-hook
if ( \'\' != $_GET[ \'file\' ] ) {
fb_get_file( $_GET[ \'file\' ] );
}
}
功能
fb_get_file()
function fb_get_file( $file ) {
$upload = wp_upload_dir();
$the_file = $file;
$file = $upload[ \'basedir\' ] . \'/\' . $file;
if ( !is_file( $file ) ) {
status_header( 404 );
die( \'404 — File not found.\' );
}
else {
$image = get_posts( array( \'post_type\' => \'attachment\', \'meta_query\' => array( array( \'key\' => \'_wp_attached_file\', \'value\' => $the_file ) ) ) );
if ( 0 < count( $image ) && 0 < $image[0] -> post_parent ) { // attachment found and parent available
if ( post_password_required( $image[0] -> post_parent ) ) { // password for the post is not available
wp_die( get_the_password_form() );// show the password form
}
$status = get_post_meta( $image[0] -> post_parent, \'_inpsyde_protect_content\', true );
if ( 1 == $status && !is_user_logged_in() ) {
wp_redirect( wp_login_url( $upload[ \'baseurl\' ] . \'/\' . $the_file ) );
die();
}
}
else {
// not a normal attachment check for thumbnail
$filename = pathinfo( $the_file );
$images = get_posts( array( \'post_type\' => \'attachment\', \'meta_query\' => array( array( \'key\' => \'_wp_attachment_metadata\', \'compare\' => \'LIKE\', \'value\' => $filename[ \'filename\' ] . \'.\' . $filename[ \'extension\' ] ) ) ) );
if ( 0 < count( $images ) ) {
foreach ( $images as $SINGLEimage ) {
$meta = wp_get_attachment_metadata( $SINGLEimage -> ID );
if ( 0 < count( $meta[ \'sizes\' ] ) ) {
$filepath = pathinfo( $meta[ \'file\' ] );
if ( $filepath[ \'dirname\' ] == $filename[ \'dirname\' ] ) {// current path of the thumbnail
foreach ( $meta[ \'sizes\' ] as $SINGLEsize ) {
if ( $filename[ \'filename\' ] . \'.\' . $filename[ \'extension\' ] == $SINGLEsize[ \'file\' ] ) {
if ( post_password_required( $SINGLEimage -> post_parent ) ) { // password for the post is not available
wp_die( get_the_password_form() );// show the password form
}
die(\'dD\');
$status = get_post_meta( $SINGLEimage -> post_parent, \'_inpsyde_protect_content\', true );
if ( 1 == $status && !is_user_logged_in() ) {
wp_redirect( wp_login_url( $upload[ \'baseurl\' ] . \'/\' . $the_file ) );
die();
}
}
}
}
}
}
}
}
}
$mime = wp_check_filetype( $file );
if( false === $mime[ \'type\' ] && function_exists( \'mime_content_type\' ) )
$mime[ \'type\' ] = mime_content_type( $file );
if( $mime[ \'type\' ] )
$mimetype = $mime[ \'type\' ];
else
$mimetype = \'image/\' . substr( $file, strrpos( $file, \'.\' ) + 1 );
header( \'Content-type: \' . $mimetype ); // always send this
if ( false === strpos( $_SERVER[\'SERVER_SOFTWARE\'], \'Microsoft-IIS\' ) )
header( \'Content-Length: \' . filesize( $file ) );
$last_modified = gmdate( \'D, d M Y H:i:s\', filemtime( $file ) );
$etag = \'"\' . md5( $last_modified ) . \'"\';
header( "Last-Modified: $last_modified GMT" );
header( \'ETag: \' . $etag );
header( \'Expires: \' . gmdate( \'D, d M Y H:i:s\', time() + 100000000 ) . \' GMT\' );
// Support for Conditional GET
$client_etag = isset( $_SERVER[\'HTTP_IF_NONE_MATCH\'] ) ? stripslashes( $_SERVER[\'HTTP_IF_NONE_MATCH\'] ) : false;
if( ! isset( $_SERVER[\'HTTP_IF_MODIFIED_SINCE\'] ) )
$_SERVER[\'HTTP_IF_MODIFIED_SINCE\'] = false;
$client_last_modified = trim( $_SERVER[\'HTTP_IF_MODIFIED_SINCE\'] );
// If string is empty, return 0. If not, attempt to parse into a timestamp
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
// Make a timestamp for our most recent modification...
$modified_timestamp = strtotime($last_modified);
if ( ( $client_last_modified && $client_etag )
? ( ( $client_modified_timestamp >= $modified_timestamp) && ( $client_etag == $etag ) )
: ( ( $client_modified_timestamp >= $modified_timestamp) || ( $client_etag == $etag ) )
) {
status_header( 304 );
exit;
}
// If we made it this far, just serve the file
readfile( $file );
die();
}
您还可以通过挂钩为文件添加自定义URL
generate_rewrite_rules
add_filter( \'generate_rewrite_rules\', \'fb_generate_rewrite_rules\' );
function fb_generate_rewrite_rules( $wprewrite ) {
$upload = wp_upload_dir();
$path = str_replace( site_url( \'/\' ), \'\', $upload[ \'baseurl\' ] );
$wprewrite -> non_wp_rules = array( $path . \'/(.*)\' => \'index.php?file=$1\' );
return $wprewrite;
}
2。Apache检查Cookie,留下新的。中的htaccess文件/wp-content/uploads/
目录或用于上载的其他定义目录。
其工作原理<IfModule>
容器,有三条规则可以执行以下操作:
检查请求是否针对任何文件检查是否缺少以开头的cookiewordpress_logged_in_
如果满足这些条件,将通过403拒绝文件请求;“禁止”;回答这里的技巧是第2步,然后检查是否缺少以开头的cookiewordpress_logged_in_
. 用户登录后,WordPress会向浏览器中添加一个cookie,如下所示:
wordpress_logged_in_1234567890abcdefghijklmnopqrstuvwxyz
检查文件类型的示例规则
# require login for media files
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} (.*)
RewriteCond %{HTTP_COOKIE} !wordpress_logged_in_([a-zA-Z0-9_]*) [NC]
RewriteRule .* - [F,L]
</IfModule>