如何仅限制通过FTP上传的PDF文件,我复制了每个PDF文件的链接并在WooCommerce中创建了按钮,我可以限制页面/帖子,但不能限制PDF。我希望用户登录后能够查看PDF文件。我将感谢您的支持。
Restrict PDF links
1 个回复
SO网友:TheGentleman
由于各种原因,您不能限制用户使用wordpress登录从路径下载文件,最重要的是WP登录由PHP处理,而下载由Apache处理。
也就是说,有一种方法可以实现你所追求的结果。您需要做的是为您的PDF编写一个自定义下载处理程序。有多种方法可以做到这一点,但它们类似于这样。
function downloadPDF(){
if(!is_user_logged_in(){
//do your user isn\'t logged in logic here
}
else{
$path = WP_CONTENT_DIR . \'/path/to/pdf\'; //The path to the pdf file that you want to access.
if(file_exists($path)){ //check to make sure the file exists
header(\'Content-Description: File Transfer\');
header(\'Content-Type: application/pdf\');
header(\'Content-Disposition: attachment; filename="\'.end(explode(\'/\',$path).\'"\');
header(\'Content-Transfer-Encoding: binary\');
header(\'Expires: 0\');
header(\'Cache-Control: must-revalidate, post-check=0, pre-check=0\');
header(\'Pragma: public\');
header(\'Content-Length: \' . filesize($path));
readfile($path);
exit();
}
else{
//do your invalid file logic here
}
}
}
这里发生的是,它检查用户是否登录。如果是,它会检查文件是否存在,以及是否提供下载服务。请注意,这是您需要做的一个非常基本的框架。当有人试图下载pdf时,您需要启动此函数(可能使用.htaccess规则+操作挂钩,但还有其他方法)。