Overwrite file_directory_temp function without touching the core? I need to modify the file_directory_temp() function, but I can't touch the core. 

My drupal-site runs in Microsoft Azure Cloud and I need to exclude all temp files from drupal-root into a absolute location.

I basically want to prefix the value from variable_get('file_temporary_path') with an absolute file-path: "C:\temp" + "\sites\default\tmp"

Because the absolute part of the path is a dynamic value, which depends on environment and is returned by function Microsoft_WindowsAzure_RoleEnvironment::getLocalResource('Temp') it can't be stored in database, due to changing enviroment.

In a nutshell: 

I need file_directory_temp() function to return:

$temporary_directory = Microsoft_WindowsAzure_RoleEnvironment::getLocalResource('Temp') . variable_get('file_temporary_path', NULL);


instead of:

$temporary_directory = variable_get('file_temporary_path', NULL);


The prefix is dynamic and can't be stored in database, due to changing enviroment setup.
Code for variable_get() is:

function variable_get($name, $default = NULL) {
  global $conf;
  return isset($conf[$name]) ? $conf[$name] : $default;
}


So if value is constant during one page call you need to set:

global $conf;
$conf['file_temporary_path'] = Microsoft_WindowsAzure_RoleEnvironment::getLocalResource('Temp') . $conf['file_temporary_path'];


If value can change for each request, you need a more bold move. And  PHP 5.2.0 or later.

class TempClass {
  private $path;
  public function __construct($path) {
    $this-&gt;path = $path;
  }
  public function __toString() {
    return Microsoft_WindowsAzure_RoleEnvironment::getLocalResource('Temp') . $this-&gt;path;
  }
}
global $conf;
$conf['file_temporary_path'] = new TempClass($conf['file_temporary_path']);


That way you'll have an object, that will be converted to string on each use, and on that conversion will retrieve current value from Microsoft_WindowsAzure_RoleEnvironment class.

Where to put it?

Best place for it seems to be a hook_boot(). You need to make sure your hook_boot() is called first. To get your module to run first, use hook_module_implements_alter() or easier and less reliable way - Modules weight module.

If you don't care about the value provided via UI, you can (maybe should) put it in settings.php instead, and substitute reads from $conf['file_temporary_path'] with hard-coded value. You don't need to use global in settings.php.

Conflict with UI

When you open form to edit file_temporary_path, you will see value with Azure's prefix string already prepended. That's not what you want there, as multiple saves would prepend it multiple times. Use hook_form_alter() to change default value to one obtained directly from the database, or, if you hardcoded it in settings.php, to set it to hardcoded string and disable field.