function FileRepositoryInterface::writeData
Same name in other branches
- 10 core/modules/file/src/FileRepositoryInterface.php \Drupal\file\FileRepositoryInterface::writeData()
- 11.x core/modules/file/src/FileRepositoryInterface.php \Drupal\file\FileRepositoryInterface::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.
Throws
\Drupal\Core\File\Exception\FileException Thrown when there is an error writing to the file system.
\Drupal\Core\File\Exception\FileExistsException Thrown when the destination exists and $replace is set to FileSystemInterface::EXISTS_ERROR.
\Drupal\Core\File\Exception\InvalidStreamWrapperException Thrown when the destination is an invalid stream wrapper.
\Drupal\Core\Entity\EntityStorageException Thrown when there is an error saving the file.
See also
\Drupal\Core\File\FileSystemInterface::saveData()
1 method overrides FileRepositoryInterface::writeData()
- FileRepository::writeData in core/
modules/ file/ src/ FileRepository.php - Writes a file to the specified destination and creates a file entity.
File
-
core/
modules/ file/ src/ FileRepositoryInterface.php, line 47
Class
- FileRepositoryInterface
- Performs file system operations and updates database records accordingly.
Namespace
Drupal\fileCode
public function writeData(string $data, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME) : FileInterface;
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.