function advagg_save_data

nittany7 advagg.missing.inc advagg_save_data($uri, $data, $overwrite = FALSE)
cis7 advagg.missing.inc advagg_save_data($uri, $data, $overwrite = FALSE)
mooc7 advagg.missing.inc advagg_save_data($uri, $data, $overwrite = FALSE)

Save data to a file.

This will use the rename operation ensuring atomic file operations.

Parameters

$uri: A string containing the destination location. This must be a stream wrapper URI.

$data: A string containing the contents of the file.

$overwrite: Bool, set to TRUE to overwrite a file.

3 calls to advagg_save_data()
advagg_htaccess_check_generate in modules/contrib/advagg/advagg.missing.inc
Generate .htaccess rules and place them in advagg dir
advagg_mod_init in modules/contrib/advagg/advagg_mod/advagg_mod.module
Implement hook_init().
advagg_save_aggregate in modules/contrib/advagg/advagg.missing.inc
Save an aggregate given a filename, the files included in it, and the type.

File

modules/contrib/advagg/advagg.missing.inc, line 638
Advanced CSS/JS aggregation module.

Code

function advagg_save_data($uri, $data, $overwrite = FALSE) {
  // File already exists.
  if (!$overwrite && file_exists($uri) && filesize($uri) > 0) {
    return;
  }

  // Perform the replace operation. Since there could be multiple processes
  // writing to the same file, the best option is to create a temporary file in
  // the same directory and then rename it to the destination. A temporary file
  // is needed if the directory is mounted on a separate machine; thus ensuring
  // the rename command stays local.

  // Get a temporary filename in the destination directory.
  $temporary_file = drupal_tempnam(drupal_dirname($uri), 'file');
  // Save to temporary filename in the destination directory.
  $filepath = file_unmanaged_save_data($data, $temporary_file, FILE_EXISTS_REPLACE);

  // Perform the rename operation.
  $result = @rename($temporary_file, $uri);
  if (!$result) {
    // Unlink and try again for windows. Rename on windows does not replace
    // the file if it already exists.
    @unlink($uri);
    $result = @rename($temporary_file, $uri);
    // Remove temporary_file if rename failed.
    if (!$result) {
      @unlink($temporary_file);
    }
  }
}