You are here

public function FileSystem::getDestinationFilename in Drupal 9

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

Determines the destination path for a file.

Parameters

string $destination: The desired final URI or filepath.

int $replace: Replace behavior when the destination file already exists.

Return value

string|bool The destination filepath, or FALSE if the file already exists and FileSystemInterface::EXISTS_ERROR is specified.

Throws

\Drupal\Core\File\Exception\FileException Implementation may throw FileException or its subtype on failure.

Overrides FileSystemInterface::getDestinationFilename

1 call to FileSystem::getDestinationFilename()
FileSystem::prepareDestination in core/lib/Drupal/Core/File/FileSystem.php
Prepares the destination for a file copy or move operation.

File

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

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function getDestinationFilename($destination, $replace) {
  $basename = $this
    ->basename($destination);
  if (!Unicode::validateUtf8($basename)) {
    throw new FileException(sprintf("Invalid filename '%s'", $basename));
  }
  if (file_exists($destination)) {
    switch ($replace) {
      case FileSystemInterface::EXISTS_REPLACE:

        // Do nothing here, we want to overwrite the existing file.
        break;
      case FileSystemInterface::EXISTS_RENAME:
        $directory = $this
          ->dirname($destination);
        $destination = $this
          ->createFilename($basename, $directory);
        break;
      case FileSystemInterface::EXISTS_ERROR:

        // Error reporting handled by calling function.
        return FALSE;
    }
  }
  return $destination;
}