Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 05:16:31 PM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
155544 Posts in 21714 Topics by 7736 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Download link for protected pdf files  (Read 770 times)
D. Peeters

Offline Offline

Posts: 128


« on: February 09, 2011, 03:33:33 PM »

Hi,

I want to put a couple of protected pdf files on my website. I don't really know how to describe what I want but it should be something like this:

I have a folder, e.g. /media/pdf/ which contains the pdf files. This folder is protected by htpasswd. Is there a script available which does the following:
check whether the user is in group 1 -> download the file via download.php?file=pdffile.pdf (without the htpasswd password?)
is the user not in group 1 or 2 -> 404

Thanks in advance!
Logged
PurpleEdge

Offline Offline

Posts: 232



WWW
« Reply #1 on: February 10, 2011, 05:17:10 AM »

Don't know if such an add-on exists, but sounds like a good candidate for one?

http://webdesign.about.com/od/php/ht/force_download.htm

http://www.higherpass.com/php/Tutorials/File-Download-Security/
Logged
D. Peeters

Offline Offline

Posts: 128


« Reply #2 on: February 10, 2011, 03:47:41 PM »

Hi,

This is what I have so far:

Code:
<?php if ( (true === $wb->is_authenticated()) && (true === in_array(1$wb->get_groups_id() ) ) ) {
// admins
header('Content-disposition: attachment; filename=Prijscourant.pdf');
                
header('Content-type: application/pdf');
                
readfile('prijscouranten/bestand.pdf');

} elseif ( (
true === $wb->is_authenticated()) && (true === in_array(2$wb->get_groups_id() ) ) ) {
// group 2
echo 'Not available to you';

} elseif ( (
true === $wb->is_authenticated()) && (true === in_array(3$wb->get_groups_id() ) ) ) {
// group 3
header('Content-disposition: attachment; filename=Prijscourant.pdf');
                
header('Content-type: application/pdf');
                
readfile('prijscouranten/bestand.pdf');

} else {
// no group
echo 'Please Log in!';

?>

I created a new webpage within WB (Code2), with a template which contains just the text (no layout whatsoever). I uploaded the pdf file to http://mywebsite.nl/pages/prices/prijscouranten/bestand.pdf because I haven't found out a solution yet to use a file from the media folder... do you know how this works?

Thanks in advance!
Logged
PurpleEdge

Offline Offline

Posts: 232



WWW
« Reply #3 on: February 11, 2011, 02:37:10 AM »

Sounds like a creative solution so far!

Does this work with the nominated file/folder?

If all you need to do is refer to a file in the Media folder, then you can use the constant MEDIA_DIRECTORY to point to that folder...

http://www.websitebaker2.org/en/help/developer-guide/variables-and-constants.php
Logged
D. Peeters

Offline Offline

Posts: 128


« Reply #4 on: February 11, 2011, 08:32:42 AM »

Yes it does grin!

But... when I changed it to MEDIA_DIRECTORY yesterday, the pdf file couldn't be opened because of the .htpasswd protection...
Logged
D. Peeters

Offline Offline

Posts: 128


« Reply #5 on: February 11, 2011, 08:41:03 AM »

Edit: I changed it to

Code:
<?php if ( (true === $wb->is_authenticated()) && (true === in_array(1$wb->get_groups_id() ) ) ) {
// admins
header('Content-disposition: attachment; filename=Prijscourant.pdf');
                
header('Content-type: application/pdf');
                
readfile('../media/prijscouranten/bestand.pdf');

} elseif ( (
true === $wb->is_authenticated()) && (true === in_array(2$wb->get_groups_id() ) ) ) {
// group 2
echo 'Not available to you';

} elseif ( (
true === $wb->is_authenticated()) && (true === in_array(3$wb->get_groups_id() ) ) ) {
// group 3
header('Content-disposition: attachment; filename=Prijscourant.pdf');
                
header('Content-type: application/pdf');
                
readfile('../media/prijscouranten/bestand.pdf');

} else {
// no group
echo 'Please Log in!';

?>


And it works cool
Logged
PurpleEdge

Offline Offline

Posts: 232



WWW
« Reply #6 on: February 12, 2011, 01:31:06 AM »

MEDIA_DIRECTORY should work - along with WB_PATH.

I always forget whether it has a trailing slash, so I frequently end up with a bad path  smiley

Good to know you got it working though!
Logged
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!