mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 4 years 9 weeks ago so it might be outdated. Please be cautious before implementing any of suggestions herein.

Drupal 7: Absolute path to file URI or file object

Say you have a file in one of your modules for which you need a file object, the trick is to _copy_ it to the public filesystem, and then use it. For example, say we need to import the french translation of MYMODULE during an update, we can use the following code:

<?php
$file
= file_save_data(file_get_contents(realpath(drupal_get_path('module', 'MYMODULE') . '/translations/fr.po')), 'public://temporary-po-import.po');
_locale_import_po($file, 'fr', LOCALE_IMPORT_OVERWRITE, 'default');
?>

Drupal will delete this file eventually because it's temporary.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.