function FileRepository::writeData

Same name and namespace in other branches
  1. 9 core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::writeData()
  2. 11.x core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::writeData()

Writes a file to the specified destination and creates a file entity.

Parameters

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

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

\Drupal\Core\File\FileExists|int $fileExists: (optional) The replace behavior when the destination file already exists.

Return value

\Drupal\file\FileInterface The file entity.

Overrides FileRepositoryInterface::writeData

File

core/modules/file/src/FileRepository.php, line 90

Class

FileRepository
Provides a file entity repository.

Namespace

Drupal\file

Code

public function writeData(string $data, string $destination, FileExists|int $fileExists = FileExists::Rename) : FileInterface {
  if (!$fileExists instanceof FileExists) {
    // @phpstan-ignore-next-line
    $fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
  }
  if (!$this->streamWrapperManager
    ->isValidUri($destination)) {
    throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
  }
  $uri = $this->fileSystem
    ->saveData($data, $destination, $fileExists);
  return $this->createOrUpdate($uri, $destination, $fileExists === FileExists::Rename);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.