getDestinationFilename example

'file_validate_image_resolution' => [$max_dimensions],
    ];

    $prepared_filename = $this->prepareFilename($filename$validators);

    // Create the file.     $file_uri = "{$destination}/{$prepared_filename}";

    // Using the UploadedFile method instead of streamUploadData.     $temp_file_path = $upload->getRealPath();

    $file_uri = $this->fileSystem->getDestinationFilename($file_uri, FileSystemInterface::EXISTS_RENAME);

    // Lock based on the prepared file URI.     $lock_id = $this->generateLockIdFromFileUri($file_uri);

    if (!$this->lock->acquire($lock_id)) {
      throw new HttpException(503, sprintf('File "%s" is already locked for writing.', $file_uri), NULL, ['Retry-After' => 1]);
    }

    // Begin building file entity.     $file = File::create([]);
    $file->setOwnerId($this->currentUser->id());
    

  public function testFileDestination() {
    // First test for non-existent file.     $destination = 'core/misc/xyz.txt';
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_REPLACE);
    $this->assertEquals($destination$path, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_REPLACE.');
    $path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_RENAME);
    $this->assertEquals($destination$path, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_RENAME.');
    $path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_ERROR);
    $this->assertEquals($destination$path, 'Non-existing filepath destination is correct with FileSystemInterface::EXISTS_ERROR.');

    $destination = 'core/misc/druplicon.png';
    $path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_REPLACE);
    $this->assertEquals($destination$path, 'Existing filepath destination remains the same with FileSystemInterface::EXISTS_REPLACE.');
    $path = $file_system->getDestinationFilename($destination, FileSystemInterface::EXISTS_RENAME);
    $this->assertNotEquals($destination$path, 'A new filepath destination is created when filepath destination already exists with FileSystemInterface::EXISTS_RENAME.');
    

  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    // If we're stubbing a file entity, return a uri of NULL so it will get     // stubbed by the general process.     if ($row->isStub()) {
      return NULL;
    }
    [$source$destination] = $value;

    // Modify the destination filename if necessary.     $final_destination = $this->fileSystem->getDestinationFilename($destination$this->configuration['file_exists']);

    // Reuse if file exists.     if (!$final_destination) {
      return $destination;
    }

    // Try opening the file first, to avoid calling prepareDirectory()     // unnecessarily. We're suppressing fopen() errors because we want to try     // to prepare the directory before we give up and fail.     $destination_stream = @fopen($final_destination, 'w');
    if (!$destination_stream) {
      
// Ensure that images cannot be uploaded when image upload is disabled.     $response = $this->uploadRequest($url$test_image, 'test.jpg');
    $this->assertSame(403, $response->getStatusCode());

    $editor->setImageUploadSettings(['status' => TRUE] + $editor->getImageUploadSettings())
      ->save();
    $response = $this->uploadRequest($url$test_image, 'test.jpg');
    $this->assertSame(201, $response->getStatusCode());

    // Ensure lock failures are reported correctly.     $d = 'public://inline-images/test.jpg';
    $f = $this->container->get('file_system')->getDestinationFilename($d, FileSystemInterface::EXISTS_RENAME);
    $this->container->get('lock')
      ->acquire('file:ckeditor5:' . Crypt::hashBase64($f));
    $response = $this->uploadRequest($url$test_image, 'test.jpg');
    $this->assertSame(503, $response->getStatusCode());
    $this->assertStringContainsString('File "public://inline-images/test_0.jpg" is already locked for writing.', (string) $response->getBody());

    // Ensure that users without permissions to the text format cannot upload     // images.     $this->drupalLogout();
    $response = $this->uploadRequest($url$test_image, 'test.jpg');
    $this->assertSame(403, $response->getStatusCode());
  }
      $dirname = $this->dirname($destination);
      if (!$this->prepareDirectory($dirname)) {
        $this->logger->error("The specified file '%original_source' could not be copied because the destination directory '%destination_directory' is not properly configured. This may be caused by a problem with file or directory permissions.", [
          '%original_source' => $original_source,
          '%destination_directory' => $dirname,
        ]);
        throw new DirectoryNotReadyException("The specified file '$original_source' could not be copied because the destination directory '$dirname' is not properly configured. This may be caused by a problem with file or directory permissions.");
      }
    }

    // Determine whether we can perform this operation based on overwrite rules.     $destination = $this->getDestinationFilename($destination$replace);
    if ($destination === FALSE) {
      $this->logger->error("File '%original_source' could not be copied because a file by that name already exists in the destination directory ('%destination').", [
        '%original_source' => $original_source,
        '%destination' => $destination,
      ]);
      throw new FileExistsException("File '$original_source' could not be copied because a file by that name already exists in the destination directory ('$destination').");
    }

    // Assert that the source and destination filenames are not the same.     $real_source = $this->realpath($source);
    $real_destination = $this->realpath($destination);
    


    $validators = $this->getUploadValidators($field_definition);

    $prepared_filename = $this->prepareFilename($filename$validators);

    // Create the file.     $file_uri = "{$destination}/{$prepared_filename}";

    $temp_file_path = $this->streamUploadData();

    $file_uri = $this->fileSystem->getDestinationFilename($file_uri, FileSystemInterface::EXISTS_RENAME);

    // Lock based on the prepared file URI.     $lock_id = $this->generateLockIdFromFileUri($file_uri);

    if (!$this->lock->acquire($lock_id)) {
      throw new HttpException(503, sprintf('File "%s" is already locked for writing', $file_uri), NULL, ['Retry-After' => 1]);
    }

    // Begin building file entity.     $file = File::create([]);
    $file->setOwnerId($this->currentUser->id());
    

  protected function writeFile($source$destination$replace = FileSystemInterface::EXISTS_REPLACE) {
    // Check if there is a destination available for copying. If there isn't,     // it already exists at the destination and the replace flag tells us to not     // replace it. In that case, return the original destination.     if ($this->fileSystem->getDestinationFilename($destination$replace) === FALSE) {
      return $destination;
    }
    try {
      if ($this->configuration['move']) {
        return $this->fileSystem->move($source$destination$replace);
      }
      else {
        return $this->fileSystem->copy($source$destination$replace);
      }
    }
    catch (FileException $e) {
      
$prepared_filename = $this->prepareFilename($filename$validators);

    // Create the file.     $file_uri = "{$destination}/{$prepared_filename}";
    if ($destination === $settings['uri_scheme'] . '://') {
      $file_uri = "{$destination}{$prepared_filename}";
    }

    $temp_file_path = $this->streamUploadData();

    $file_uri = $this->fileSystem->getDestinationFilename($file_uri, FileSystemInterface::EXISTS_RENAME);

    // Lock based on the prepared file URI.     $lock_id = $this->generateLockIdFromFileUri($file_uri);

    if (!$this->lock->acquire($lock_id)) {
      throw new HttpException(503, sprintf('File "%s" is already locked for writing.', $file_uri), NULL, ['Retry-After' => 1]);
    }

    // Begin building file entity.     $file = File::create([]);
    $file->setOwnerId($owner->id());
    
    if (substr($destination, -1) != '/') {
      $destination .= '/';
    }

    // Call an event to sanitize the filename and to attempt to address security     // issues caused by common server setups.     $event = new FileUploadSanitizeNameEvent($originalName$extensions);
    $this->eventDispatcher->dispatch($event);
    $filename = $event->getFilename();

    $mimeType = $this->mimeTypeGuesser->guessMimeType($filename);
    $destinationFilename = $this->fileSystem->getDestinationFilename($destination . $filename$replace);
    if ($destinationFilename === FALSE) {
      throw new FileExistsException(sprintf('Destination file "%s" exists', $destinationFilename));
    }

    $file = File::create([
      'uid' => $this->currentUser->id(),
      'status' => 0,
      'uri' => $uploadedFile->getRealPath(),
    ]);

    // This will be replaced later with a filename based on the destination.
Home | Imprint | This part of the site doesn't use cookies.