function FileRepository::writeData
Same name and namespace in other branches
- 10 core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::writeData()
- 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.
int $replace: (optional) The replace behavior when the destination file already exists. Possible values include:
- FileSystemInterface::EXISTS_RENAME: (default) Append _{incrementing number} until the filename is unique.
- FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a managed file with the destination name exists, then its database entry will be updated. If no database entry is found, then a new one will be created.
- FileSystemInterface::EXISTS_ERROR: Do nothing and throw a \Drupal\Core\File\Exception\FileExistsException.
Return value
\Drupal\file\FileInterface The file entity.
Overrides FileRepositoryInterface::writeData
File
-
core/
modules/ file/ src/ FileRepository.php, line 89
Class
- FileRepository
- Provides a file entity repository.
Namespace
Drupal\fileCode
public function writeData(string $data, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME) : FileInterface {
if (!$this->streamWrapperManager
->isValidUri($destination)) {
throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
}
$uri = $this->fileSystem
->saveData($data, $destination, $replace);
return $this->createOrUpdate($uri, $destination, $replace === FileSystemInterface::EXISTS_RENAME);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.