file_unmanaged_save_data

Definition

file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME)
drupal/includes/file.inc, line 1172

Description

Save a string to the specified destination without calling any hooks or making any changes to the database.

This function is identical to file_save_data() except the file will not be saved to the files table and none of the file_* hooks will be called.

@see file_save_data()

Parameters

$data A string containing the contents of the file.

$destination A string containing the destination location. If no value is provided then a randomly name will be generated and the file saved in Drupal's files directory.

$replace Replace behavior when the destination file already exists:

  • FILE_EXISTS_REPLACE - Replace the existing file.
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.

Return value

A string with the path of the resulting file, or FALSE on error.

Related topics

Namesort iconDescription
File interfaceCommon file handling functions.

Code

function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  // Write the data to a temporary file.
  $temp_name = tempnam(file_directory_temp(), 'file');
  if (file_put_contents($temp_name, $data) === FALSE) {
    drupal_set_message(t('The file could not be created.'), 'error');
    return FALSE;
  }

  // Move the file to its final destination.
  return file_unmanaged_move($temp_name, $destination, $replace);
}