You are here

function boost_write_file in Boost 7

Write to a file. Ensures write is atomic via rename operation.

Parameters

$filename: relative filename.

$data: data to write to the file.

2 calls to boost_write_file()
boost_exit in ./boost.module
Implements hook_exit().
boost_htaccess_cache_dir_put in ./boost.module
Overwrite old htaccess rules with new ones.

File

./boost.module, line 1369
Caches generated output as a static file to be served directly from the webserver.

Code

function boost_write_file($filename, $data) {

  // Create directory if it doesn't exist.
  $directory = dirname($filename);
  if (!boost_mkdir($directory)) {
    return FALSE;
  }

  // Save data to a temp file.
  // file_unmanaged_save_data does not use rename.
  $tempname = drupal_tempnam($directory, 'boost');
  if (file_put_contents($tempname, $data) === FALSE) {
    watchdog('boost', 'Could not create the file %file on your system', array(
      '%file' => $tempname,
    ), WATCHDOG_ERROR);
    @unlink($tempname);
    return FALSE;
  }

  // Move temp file to real filename; windows can not do a rename replace.
  if (@rename($tempname, $filename) === FALSE) {
    $oldname = $tempname . 'old';
    if (@rename($filename, $oldname) !== FALSE) {
      if (@rename($tempname, $filename) === FALSE) {
        watchdog('boost', 'Could not rename the file %file on your system', array(
          '%file' => $filename,
        ), WATCHDOG_ERROR);
        @unlink($tempname);
        @rename($oldname, $filename);
        return FALSE;
      }
      else {
        @unlink($oldname);
      }
    }
  }

  // chmod file so webserver can send it out.
  drupal_chmod($filename);
  return TRUE;
}