public function FileRepositoryInterface::move in Drupal 10
Moves a file to a new location and update the file's database entry.
- Checks if $source and $destination are valid and readable/writable.
- Performs a file move if $source is not equal to $destination.
- If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
- Adds the new file to the files database.
Parameters
\Drupal\file\FileInterface $source: A file entity.
string $destination: A string containing the destination that $source should be moved to. 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 and $source->delete() called after invoking hook_file_move(). If no database entry is found, then the source files record will be updated.
- 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::move()
File
- core/
modules/ file/ src/ FileRepositoryInterface.php, line 144
Class
- FileRepositoryInterface
- Performs file system operations and updates database records accordingly.
Namespace
Drupal\fileCode
public function move(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME) : FileInterface;