Drupal 7 File Stream Wrapper Translation

18th October 2013

Steam wrappers were introduced in Drupal 7 and allow user file locations to be kept in a maintainable way, although I often forget which function to use to translate them. The three wrappers available are public://, private://, and temporary://, which map to the public, private, and temporary files directories respectively. All user files in Drupal are stored in either of these directories and they are referenced in the database as the file wrapper followed by the location of the file. This means that the location of the files is only dependent on a single config setting.

Translation of the stream wrapper into a file location is dependent on one of two functions, depending on what sort of file location you want. If you want to get a local file location then you would use drupal_realpath(), whereas if you want a URL of the file you would use file_create_url().

For example, the a file called test.png in the public file directory might have be presented as public://test.png, this would be translated in the following ways by the file_create_url() and drupal_realpath() functions.

  1. $url = file_create_url('public://test.png');
  2. print $url; // prints 'http://www.example.com/sites/default/files/test.png'
  3. $location = drupal_realpath('public://test.png');
  4. print $location; // prints '/path/to/docroot/sites/default/files/test.png'

If the file is kept in the private files directory (and Drupal has been configured to use this) then the output will be slightly different for the file_create_url() function. This is because private files must go through a layer of authentication and so the function doesn't link directly to the file.

  1. $url = file_create_url('private://test.png');
  2. print $url; // prints 'http://www.example.com/system/files/test.png'

The temporary:// stream wrapper is used to reference the temporary directory, which is set in the Drupal configuration. This generally shouldn't be available and therefore shouldn't be accessible as a URL.

  1. $location = drupal_realpath('temporary://test.png');
  2. print $location; // prints '/tmp/test.png'

The drupal_realpath() function is also a handy way in which to get hold of the user files directory by just passing it the stream wrapper on it's own.

  1. $location = drupal_realpath('public://');
  2. print $location; // prints '/path/to/docroot/sites/default/files/'

You will find these stream wrappers being used all over Drupal where files are used. The following code is used to extract an image from an iamge field on a Drupal node and then inspect the data held within.

  1. $image = array_pop(field_get_items('node', $node, 'field_image'));
  2. print '' . print_r($image, true) . '';

This prints the following output. Notice that the file is called image.jpg and that it is held at 'public://image.jpg'.

  1. Array
  2. (
  3. [fid] => 9
  4. [alt] =>
  5. [title] =>
  6. [width] => 380
  7. [height] => 253
  8. [uid] => 1
  9. [filename] => image.jpg
  10. [uri] => public://image.jpg
  11. [filemime] => image/jpeg
  12. [filesize] => 42649
  13. [status] => 1
  14. [timestamp] => 1372157519
  15. )

To get the full URL of the image you would translate the uri element of the image array like this.

  1. print file_create_url($image['uri']);
  2. // prints 'http://www.example.com/sites/default/files/image.jpg'

Comments

Permalink
Something for Drupal translators and also developers: https://poeditor.com/. It's very simple to use and it has a Drupal plugin that makes import and eport much simpler. Crowd translation is perfect this way.

Submitted by Bandi on Thu, 12/12/2013 - 13:23

Permalink
Thanks, but it's not that kind of translation...

Submitted by philipnorton42 on Thu, 12/12/2013 - 20:27

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.