那里有一个头衔。
关注这篇精彩帖子:https://wordpress.stackexchange.com/questions/37144/how-to-protect-uploads-if-user-is-not-logged-in我创建了一个htaccess规则来重定向某个目录:
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/indres(.*)$ dl-file-indres.php?file=$1 [QSA,L]
到php身份验证脚本(从原始帖子稍微改动一下,以反映WP默认上传路径中的目录):
<?php
require_once('wp-load.php');
current_user_can('edit_posts') || auth_redirect();
list($basedir) = array_values(array_intersect_key(wp_upload_dir(), array('basedir' => 1)))+array(NULL);
$file = rtrim($basedir,'/').'/indres/'.str_replace('..', '', isset($_GET[ 'file' ])?$_GET[ 'file' ]:'');
if (!$basedir || !is_file($file)) {
status_header(404);
die('404 — File not found.');
}
$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 );
所以这完全符合预期,除了一个大问题:所有pdf都被破坏了,无论我使用什么浏览器,即使我强迫用户使用{{1}下载文件}。它将下载大约90%并显示一条消息,表明它没有正确显示。如果我注释掉AddType application/octet-stream .pdf
规则,则pdf显示正常。
在我看来,这个问题存在于php脚本本身中,但是我对脚本中的内容导致它感到茫然。有人有主意吗?非常感谢,
答案 0 :(得分:0)
结果发现冲突与WP Minify
插件有关。不知道为什么,但那是罪魁祸首。