mediatribe.net -- Drupal and Web Development

Notice: this post was last updated 4 years 21 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.