Forum

Thread tagged as: Configuration, Addons, Members

Secure Downloads

Apologies, but I just cannot seem to get my head round setting up secure downloads through the members app. I followed the documentation for the members app and can create users, login etc, but am unable to use the app to upload or download secure files - I'm obviously missing something.

I have created a folder called 'secure' that is one level up from public_html. This secure folder contains a test.txt file which I have manually inserted.

Now what? How do I download that file, going to members/dowload.php just gives the white page of death. Here is my download.php file:

<?php 
    include('../perch/runtime.php'); 

    // config
    $bucket_name = 'secure';
    $url_param   = 'file';


    // By default, deny downloads unless we've proved the member is allowed this file.
    $allow_download = false;


    // Check a member is logged in
    if (perch_member_logged_in()) {
        $allow_download = true;
    }

    /*
    Alternatively, you could check for a specific tag, e.g.

    if (perch_member_has_tag('subscriber')) {
        $allow_download = true;
    }

    */


    // Deliver the file.
    if ($allow_download) {
        perch_members_secure_download(perch_get($url_param), $bucket_name);
    }

    exit;
?>

Also, how are files uploaded to the 'secure' folder? Is this done through the members app (I can't see an 'upload' box). I have created a template called 'secure_file.html', which contains the following code but I'm not then sure what to do with it.

<a href="<perch:content id="file" type="file" label="File" order="2" bucket="secure" />">
      <perch:content type="text" id="desc" label="Title" order="1" required="true" title="true" />
  </a>

This has been driving me mad all day, thanks in advance! :)

Ian Rock

Ian Rock 0 points

  • 7 years ago

Ian. I will watch this post and if Drew or Rachel are unable to get you going on this I will assist. I have become quite familiar with secure downloads lately.

Robert

Thanks Robert!

Ian,

Here is how I would setup your "upload" region if I were doing this.

On members/index.php I would do the following after perch runtime include...

perch_content_create('Secure-Downloads', array(
    'template' => 'secure_file.html',
    'multiple' => true,
    'edit-mode' => 'listdetail',
)); 

Then you will have the "upload" region available in perch.

Next, you will have to create a region

perch_content('Secure-Downloads')

which will display the available downloads to the logged in member. Of course you will need to do some setup to make sure the member is logged in before showing the region.

Does this help?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to pass in the file you want to download to the script

members/dowload.php?file=test.txt

Thanks guys.

Right, using the above code, I have managed to get a place on the back end which allows me to upload files to a secure folder outside of the web root.

However, the files are unable to be accessed from the web page themselves when logged in. They give a 404 as the links are looking for the files within the web root.

Drew, do you have a working code base that I can look at that uses the members app with secure downloads? Thanks.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What URL do you get when you have a 404, and what should it be?

download.php should be in your site - it delivers files from outside of the webroot.

The link points to mysite.local/members/download.php?file=/2014-06-13.jpg.

The source for the above link is actually blank, rather than a 404. The file 2014-06-13.jpg exists in a folder called 'secure' which is one up from root. The code for download.php is unchanged from my first post.

My buckets.php file is as follows

<?php 
return array(
    'secure' => array(
        'web_path'  => '/members/download.php?file=',
        'file_path' => '/pathtoprojectfolder/secure',
    ),
);
?>

Thanks!

The download.php Will return to a blank page if you are not logged in as a member. I have had to create a member ship and login to that before using the download script.

Thanks for that Robert, however I am currently logged in and still get the blank page. I followed your suggestions above and my members/index.php file is as follows:

<?php include('../perch/runtime.php'); 
    perch_content_create('Secure-Downloads', array(
        'template' => 'secure_file.html',
        'multiple' => true,
        'edit-mode' => 'listdetail',
    )); 
?>
<?php perch_layout('global.header'); ?>

    <div class="wrapper">

        <section id="main_text">

        <?php 
            if (perch_member_logged_in()) {
                echo '<h1>Membership area, welcome back '.perch_member_get('first_name').'!</h1>';
                perch_content('Secure-Downloads');
            }else{
                echo '<h1>Membership area.</h1>';
                echo '<p>You are not logged in. Please login.</p>';
            }
        ?>

        </section>

<!-- etc, etc... -->

Ian are you logged in to perch or are you logged in as a new member as well as perch?

The download.php script has an if statement for logged in members. Even as an admin I have never been able to use downloads PHP without also being logged into perch members. I actually have a cute little workaround for this and I will post it back here as soon as I can get to a computer rather than my phone.

Yes Robert, I'm logged into members app. Just tried logging into members only on another browser and still having the blank page issue.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is there anything in your server's PHP error log?