4

I have set up files under private filesystem (large tree of dirs and files). Drupal does not know anything about those files, because they are not added by file upload field, but links are manually defined as strings (in special field in content type), and put in place with ftp.

Also, path to those files are not completely contained within string, but constructed in field--field_myfiled.tpl.php (some taxonomy magic adds parts of the path, so in actual string i have only filename).

Now, adding just system/files/ before link constructed in field--field_myfiled.tpl.php returns privilege errors, so I guess i need to tell Drupal somehow to allow downloading these files. I am not sure - could I use hook_file_download in this case, and how I could implement this or simmilar function or approach to give access to those files ?

EDIT: My latest findings are that it seems that hook_file_download is triggered whenever you try to access a valid link to a file in private filesystem. No need for anything except hook_file_download here!

1
+50

Here are a few modules that should help you configure the way files are served.

  • Storage API module. It provides advanced file storage and serving options; such as to upload or serve from a separate server, via ftp, etc. It's my favorite option. I currently use it to host all files on a separate server.

  • CDN module. Designed for CDN related tasks, but can be used in your case to specify an alternative file location for specific file types.

  • Remote Steam Wrapper module. Not sure if this will work for private files. It makes it easier to serve external files.

  • Media Mover module. Provides advanced file serving possibilities, i.e. get files from a FTP server, provide file tokens for authentication, get files from CCK fields, etc. It's only available in development version.

Update

If you want to do it via custom code, use field_view_value() function to pull the URL from the field. Use file_create_url to create URL for streaming. You'll also need to use hook_file_download to allow the file to be downloaded; otherwise you will get a page not found.

Update

If I understood correctly, you have a field, where you enter the file name (i.e. myFile.pdf), and you rewrote its field theme (field--field_myfield.tpl.php) to generate the full location of the file. The problem here, is that you cannot include hooks/functions within a theme. Your solution would be to construct the file location in a custom module or template.php.

Example of rewriting the field value via template.php. Replace MYTHEME with your theme name.

function MYTHEME_preprocess_field(&$vars) {
  if($vars['element']['#field_name'] == 'field_myField') {
    $filePath = '/secret/location/' . $vars['items']['0']['#markup'];
    $vars['items']['0']['#markup'] = file_create_url($filePath);
    return;
  }
}

If you want to check for permissions, use hook_file_download.

| improve this answer | |
  • Thanks, information from your Update section is guidance what I need. I am not quite sure though, how I could in my custom code "register" links that will be put through hook_file_download ? – NenadP Mar 28 '13 at 12:19
  • problem is that drupal does not know for those files I am putting for download (those files does not have any db entry - they are just linked). Is there a way to construct "on the fly" data that will go through hook_file_download ? – NenadP Mar 28 '13 at 12:34
  • See my update, above. – timofey.com Mar 28 '13 at 22:06
  • Thanks, it seems that this may lead me to the solution! I will need to test it. Meanwhile enjoy the bounty reward. ;) – NenadP Mar 29 '13 at 9:20
  • 1
    Glad I could help:) – timofey.com Mar 29 '13 at 19:52
0

First i would make sure you can access these files directly via system/files/myfile.txt.

If it doesnt work, you'll have to check your .htaccess file and enable that directory for public viewing or just 755 permissions that folder.

if that does work, then I would create a View as either a block or page depending on the flexibility you need and add in the fields or terms associated with your tpl and create another text related field and rewrite the results of the previous field to construct your links using tokens.

| improve this answer | |
  • 2
    He is trying to avoid having the files publicly downloadable as far as I can tell, which means he has to deny them with .htaccess. He is looking for a way to serve them through Drupal without Drupal being 'aware' of them. – Phizes Mar 23 '13 at 19:02
  • Phizes is true - files are located out of the webroot. Also, I would like to avoid Views and make the solution as generic as possible (via custom module). Tnx for your effort! – NenadP Mar 24 '13 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.