PHP Web Development Wordpress

WordPress page template to access htaccess protected pdf file

WordPress page template to access htaccess protected pdf file:

<?php
/* pdf file download provision */
$download_success = false;
$file_found = false;
if(current_user_can( ‘manage_options’ )){
$doc = esc_url($_GET[‘doc’]);
echo $file = str_replace(‘https://wma.ie/’,ABSPATH ,$doc);
if (file_exists($file)) {
$file_found = true;
ob_clean();
flush();
header(‘Content-Description: File Transfer’);
header(‘Content-Type: application/octet-stream’);
header(‘Content-Disposition: attachment; filename=’.basename($file));
header(‘Content-Transfer-Encoding: binary’);
header(‘Expires: 0’);
header(‘Cache-Control: must-revalidate’);
header(‘Pragma: public’);
header(‘Content-Length: ‘ . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
$download_success = true;
}
/* /pdf file download provision */
?>
<?php
/*
Template name: PDF View
*/
get_header();
?>
<!– ================================= START CONTENT === –>
<div class=”content”>
<div class=”container”>
<?php while(have_posts()): the_post(); ?>
<div class=”blog”>
<div class=”row”>
<div class=”span12″>
<div class=”blog_article”>
<div class=”entry-header”>
<h3><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h3>
</div>
<div class=”entry-content”>
<?php
if($download_success == false){?>
<div class=”alert alert-danger”>You are not authorised to view the content</div>
<?php }else if($file_found == false){ ?>
<div class=”alert alert-danger”>No such file found!</div>
<?php }else{
the_content();
} ?>

</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<!– ================================= END CONTENT === –>
<?php get_footer(); ?>

 

Htaccess code to deny pdf access:

Order Allow,Deny
Allow from all
<Files ~ “\.(docx|doc|pdf)$”>
Deny from all
</Files>

Leave a Reply

Your email address will not be published. Required fields are marked *


CAPTCHA Image
Reload Image