Handling of public and private files in Drupal

Joeri Poesen //

This article relates to Drupal 5.x

Drupal can handle public and private files. It’s quite good at this. However, when displaying a link to your files, there’s something you should be aware of:

Do not:

<?php 
  print l('somefile', $node->files[0]->filepath); 
?>

Do:

<?php 
  print l('somefile', file_create_url($node->files[0]->filepath)); 
?>

The former will work as long as Drupal’s file system is set to public. Once set to private, it will make you cry. Really.

The latter will work for both public and private file system settings.