Skip to main content
Beacon9.ca
interactive media for the web

Main menu

  • Home
  • Portfolio
  • Labs
  • Contact

Search form

Drupal 7 Private Files Module

I've put together this module to extend Drupal's functionality giving a truly private file system. Currently, Drupal will let you choose to store your uploaded files using the public or private file system however, even if you chose private it is still possible to browse to these files with a URL. I've come across a few situations where I've had files which should only be viewable by logged in users (e.g. user pics). In other situations I've needed to make sure certain files are only viewable by the user that uploaded them.

Private Files Module lets you do all this without breaking a sweat.

Using the module

  • Install Private Files module from module list.
  • Setup user permissions at admin/people/permissions
  • Choose security type for each folder at admin/config/media/private-files

Let me know what you think.

File Attachment: 
private_files.zip

Spread The Love

Tweet
  • Add new comment

Comments

Steve's picture

With the very limited testing

Permalink Submitted by Steve (not verified) on Wed, 05/04/2011 - 15:39

With the very limited testing that I've done this seems to do exactly what I want, which is to restrict private files to logged-in users (at this point I have no need to restrict access to the user who uploaded the file so I haven't tested that function).

Thanks! This was a showstopper and I was in the early stages of planning a switch away from Drupal.

  • reply
jack's picture

fantastic... glad to hear it

Permalink Submitted by jack on Wed, 05/04/2011 - 16:22

fantastic... glad to hear it helped you.

  • reply
jack's picture

I'm looking for ideas on how

Permalink Submitted by jack on Mon, 05/09/2011 - 11:56

I'm looking for ideas on how to improve the module. Would appreciate any feedback you can provide!

  • reply
Marshall's picture

Have you uploading this as a

Permalink Submitted by Marshall (not verified) on Wed, 05/11/2011 - 12:58

Have you uploading this as a module at drupal.org?

  • reply
jack's picture

Hi Marshall - I haven't yet.

Permalink Submitted by jack on Wed, 05/11/2011 - 13:09

Hi Marshall - I haven't yet. I still haven't taken the time to figure out GIT yet, but I'm working on it.

  • reply
Marshall's picture

Yeah, this module is magical.

Permalink Submitted by Marshall (not verified) on Wed, 05/11/2011 - 14:50

Yeah, this module is magical. If I had known about it, I could have saved myself 2 days of trying to get Drupal to see a folder outside of the site root. Media Temple hates me. I hope you're able to get this up soon and thanks.

  • reply
Sutharsan's picture

Nice work, and so simple. Why

Permalink Submitted by Sutharsan (not verified) on Thu, 05/12/2011 - 06:33

Nice work, and so simple. Why not share this code at drupal.org?

  • reply
jack's picture

Sutharsan - glad you like it.

Permalink Submitted by jack on Thu, 05/12/2011 - 09:11

Sutharsan - glad you like it. As I mentioned in a comment above... I'm still trying to figure out GIT well enough to add a module onto Drupal.org. I'll get there eventually ;-)

  • reply
Sutharsan's picture

Totally off topic, but I

Permalink Submitted by Sutharsan (not verified) on Thu, 05/12/2011 - 09:44

Totally off topic, but I recommend the book 'Pragmatic Version Control Using Git'. It helped me a lot. Wrapping my head around the branches and the workflow was the toughest thing to do.

  • reply
jack's picture

awesome! thanks for the tip.

Permalink Submitted by jack on Thu, 05/12/2011 - 12:36

awesome! thanks for the tip.

  • reply
Sutharsan's picture

I hate to ruin a party, but

Permalink Submitted by Sutharsan (not verified) on Mon, 05/30/2011 - 17:51

I hate to ruin a party, but after long struggling, debugging and testing with various access control modules in combination with private files I came to the conclusion that this module is not needed to block access to private files which are uploaded via an image field on an access restricted node. This images are not loaded as part of the restricted node, but also not accessible via the (example.com/system/...) url. It did not work at first, the access to the node was restricted but the file access was permitted (using TAC Lite). But when I started over again with TAC Lite it worked as advertised. Also with the Node Privacy by Role module. Can't explain why it did not work in the first place but my use case for this Private Files module is no longer there.
Thanks any way for the effort of writing the code and sharing it.

  • reply
jack's picture

Hey - Not sure I 100%

Permalink Submitted by jack on Mon, 05/30/2011 - 23:05

Hey - Not sure I 100% understand your scenario but I'm glad you made it work without the module... a simpler solution with less code is almost always a good thing.

  • reply
Marshall's picture

I had the same thing going.

Permalink Submitted by Marshall (not verified) on Tue, 05/31/2011 - 18:47

I had the same thing going. All my files were private on my MAMP installation of a site. Then, when I moved to a staging server the file privacy stopped working and the only way I could get it back while not totally re-doing my file structure was to use this module.

  • reply
Bas's picture

Hi Erik, it may solve that

Permalink Submitted by Bas (not verified) on Mon, 10/24/2011 - 03:29

Hi Erik, it may solve that particular use case but if you're using CKEditor a good private directory control module is much needed.

  • reply
Renaee's picture

Hi there,

Permalink Submitted by Renaee (not verified) on Thu, 06/02/2011 - 04:51

Hi there,
I do not see anything at the admin config area:
admin/config/media/private-files

the overlay pops up but there is nothing there.

Any explanation?
many thanks,
Renaee.

  • reply
mori's picture

Did you enable the private

Permalink Submitted by mori (not verified) on Thu, 11/03/2011 - 11:51

Did you enable the private file system?
And are there any subfolders into the main private root directory?
If not then nothing can be displayed.

  • reply
Sutharsan's picture

I'll explain my scenario: A

Permalink Submitted by Sutharsan (not verified) on Fri, 06/03/2011 - 06:15

I'll explain my scenario: A node with an image field. Access to the node is controlled with TAC Lite. The image is stored in the private file system.

I expected the image to have the same access restrictions as the node. Thus the URL of the image to give a 403 for the same roles as the node does.

I think my problem was fixed by the latest security release of Drupal. It worked as expected after upgrade to 7.2. See http://drupal.org/node/1168756

  • reply
Steffen's picture

Thank you for this module!

Permalink Submitted by Steffen (not verified) on Tue, 07/05/2011 - 08:31

Thank you for this module! Needed to restrict access to a filefield file – fieldpermissions don't worked.
But since drupal 7 is able to deal with private and public files at the same time, perhaps the "file_default_scheme() == 'public'" isn't needed. I've removed it and it's working like expected for the private files.
Perhaps this module can extend fieldpermissions? You should release it on drupal.org, it's a gem.

  • reply
Carl's picture

This is great - I've been

Permalink Submitted by Carl (not verified) on Tue, 09/06/2011 - 10:32

This is great - I've been looking for a solution to this problem for literally months now but have only just come across your page.
I've tested the module out and it seems to work OK. Perhaps lacking a bit of contextual info in terms of UI, but it's so straight forward that it is still perfectly usable without.

My one question is this: If I set a folder to only allow access to authenticated users, and an anonymous user accesses the direct URL of a file within that directory they are presented with an access denied page - My site is configured such that this page also has a log in form on it. If the user then logs in, they are instantly able to download the file, but they're left on the 'access denied' page. Do you know if there is a relatively simple way to redirect the user once they login, so that they are taken to the node page of the the node to which the file has been uploaded, as well as /or instead of just downloading the file? In your hook_file_download is see that you find out which folder is being requested, but the return values for this function don't allow for this sort of functionality. I wonder if there's a way to back trace this directory to the corresponding node? Totally outside of the scope of this module, I'll admit, but I just thought I'd try and pick your brains!

  • reply
jack's picture

Hey - not sure about the

Permalink Submitted by jack on Thu, 10/13/2011 - 20:04

Hey - not sure about the redirect thing. There used to be a module called Login Toboggan that did that. Might be worth a quick look on drupal.org for it.

  • reply
Bas's picture

Hi, thanks for sharing this!

Permalink Submitted by Bas (not verified) on Mon, 10/24/2011 - 03:25

Hi, thanks for sharing this! I think this functionality should have been in Drupal core. Because it is so trivial, any module should be well-configurable so that (almost) all situations could be solved with it.

Your problem is handy, but it lacks good configuration right now. Right now, this module is not usable for me yet. I have configured CKEditor to have a private folder for every individual user. This creates directories in de private folder named with the user id (for example '456'). By default, new folders accessible by your module and though it is possible to change the settings of existing folders, I want to specify a 'default' setting for new folders. It would be best to specify that only directories with digits are accessible by people who can read ckeditor content.

I think it would be better if you could specify regular expressions to match paths within the private folder. You should be able to sort these rules, such that you can specify which reg exp is more important that the other. This would make the module better configurable.

Per reg exp you should be able to configure:
- if owner users should be able to see at (checkbox)
- the permission that the user should 'own' to view this file
(users should be able to create new permissions dynamically)

Do you have a problem with me changing your module to make it configurable like above, when I have time for it? I kind of need this quite quickly so prefer to create it myself.

  • reply
jack's picture

Hey - I have no problem with

Permalink Submitted by jack on Tue, 10/25/2011 - 10:33

Hey - I have no problem with you modifying the module... let me know if you have any questions.

  • reply
mori's picture

First of all: congrats for

Permalink Submitted by mori (not verified) on Mon, 10/31/2011 - 12:23

First of all: congrats for that excellent work !!!

Currently with D7.9, D7.7 works, I don´t see the options for the folders on the admin-page. Maybe this is caused due to another module?

  • reply
mori's picture

Everything perfect, it was

Permalink Submitted by mori (not verified) on Mon, 10/31/2011 - 12:28

Everything perfect, it was just a setup mistake to an new empty private folder. SORRY.

  • reply
jack's picture

glad you got it figured out!

Permalink Submitted by jack on Tue, 11/01/2011 - 15:44

glad you got it figured out!

  • reply
mori's picture

Someone set up this project

Permalink Submitted by mori (not verified) on Thu, 11/03/2011 - 11:52

Someone set up this project based on your work:
http://drupal.org/project/private_files_download_permission

  • reply
mori's picture

Maybe this is an issue having

Permalink Submitted by mori (not verified) on Thu, 11/03/2011 - 11:58

Maybe this is an issue having a look at.

For a music site I wanted to use the private file system and the media audio field with the WordPress Audio Player and protect the audio files with your module.

But when I restrict access to the folder for logged in users the player is not able to play the files anymore. Instead the player says "file not found".

Maybe a solution is to use the jPlayer which has an admin option to protect the audio files. But yet I did not try it because of some bugs.

The site with the player is here: http://www.luvliterecordings.com

  • reply
Debra's picture

This was exactly what I

Permalink Submitted by Debra (not verified) on Wed, 11/30/2011 - 00:57

This was exactly what I needed in a crunch. You saved me loads of headaches and time. Thanks!

  • reply
Jacob's picture

Hello!

Permalink Submitted by Jacob (not verified) on Thu, 12/08/2011 - 12:44

Hello!
Please make a redirect to the login page when a unregistered user tries to download the file!

Thank you!

  • reply
SlyK's picture

Hello. Thanks for module!

Permalink Submitted by SlyK (not verified) on Thu, 02/02/2012 - 07:11

Hello. Thanks for module!
There are some fix was needed for me. My default file scheme is 'public', but I use some 'private' file fields. So there is no need to check my default file scheme.
if(file_default_scheme() == 'public') { - not needed

  • reply

Add new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.

More information about text formats

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Email Facebook RSS

From the Labs

  • Drupal Social Popularity Module
    | 26 Nov
  • Drupal 7 Facebook Comments
    | 08 Nov
  • Drupal Mobile Redirect Module
    | 24 May
  • Drupal 7 Private Files Module
    | 30 Apr
  • Drupal 7 Sitemap Block Module
    | 11 Dec

View Labs

From The Portfolio

View Portfolio

Rank us on #rankable

Copyright © 2009-2010 BEACON9 All rights reserved. Admin Login Powered by Drupal