You are here

public function FileSystem::saveData in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::saveData()
  2. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::saveData()

File

core/lib/Drupal/Core/File/FileSystem.php, line 504

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function saveData($data, $destination, $replace = self::EXISTS_RENAME) {

  // Write the data to a temporary file.
  $temp_name = $this
    ->tempnam('temporary://', 'file');
  if (file_put_contents($temp_name, $data) === FALSE) {
    $this->logger
      ->error("Temporary file '%temp_name' could not be created.", [
      '%temp_name' => $temp_name,
    ]);
    throw new FileWriteException("Temporary file '{$temp_name}' could not be created.");
  }

  // Move the file to its final destination.
  return $this
    ->move($temp_name, $destination, $replace);
}