Same name and namespace in other branches
  1. 4.6.x includes/file.inc \file_save_data()
  2. 4.7.x includes/file.inc \file_save_data()
  3. 5.x includes/file.inc \file_save_data()
  4. 7.x includes/file.inc \file_save_data()
  5. 8.9.x core/modules/file/file.module \file_save_data()
  6. 9 core/modules/file/file.module \file_save_data()

Save a string to the specified destination.

Parameters

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

$dest A string containing the destination location.:

$replace Replace behavior when the destination file already exists.:

Return value

A string containing the resulting filename or 0 on error

Related topics

3 calls to file_save_data()
drupal_build_css_cache in includes/common.inc
Aggregate and optimize CSS files, putting them in the files directory.
drupal_build_js_cache in includes/common.inc
Aggregate JS files, putting them in the files directory.
_color_save_stylesheet in modules/color/color.module
Save the rewritten stylesheet to disk.

File

includes/file.inc, line 877
API for handling file uploads and server file management.

Code

function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) {
  $temp = file_directory_temp();

  // On Windows, tempnam() requires an absolute path, so we use realpath().
  $file = tempnam(realpath($temp), 'file');
  if (!($fp = fopen($file, 'wb'))) {
    drupal_set_message(t('The file could not be created.'), 'error');
    return 0;
  }
  fwrite($fp, $data);
  fclose($fp);
  if (!file_move($file, $dest, $replace)) {
    return 0;
  }
  return $file;
}