Skip to content

Drupal\helper\File

Provides helpers for working with files.

Methods

Name Description
__construct File constructor.
createOrReuseFromUri Creates or reuses a file object based on a URI.
filterValidFiles Filter callback; Permanent files or temporary files owned by current user.
getDataUri Converts a file URL into a data URI.

File::__construct

Description

public __construct (\Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager, \Symfony\Component\Mime\MimeTypeGuesserInterface $mime_type_guesser)

File constructor.

Parameters

  • (\Drupal\Core\Entity\EntityTypeManagerInterface) $entity_type_manager : The entity type manager.
  • (\Symfony\Component\Mime\MimeTypeGuesserInterface) $mime_type_guesser : The mime type guesser.

Return Values

void


File::createOrReuseFromUri

Description

public createOrReuseFromUri (string $uri, bool $reuse_existing)

Creates or reuses a file object based on a URI.

Parameters

  • (string) $uri : A string containing the URI.
  • (bool) $reuse_existing : If TRUE will try to reuse an existing file with the same URI.
    If FALSE will always create a new file.

Return Values

\Drupal\file\FileInterface

A file object.


File::filterValidFiles

Description

public static filterValidFiles (\Drupal\file\FileInterface $file)

Filter callback; Permanent files or temporary files owned by current user.

Parameters

  • (\Drupal\file\FileInterface) $file : The file object.

Return Values

bool

TRUE if the file is valid, or FALSE otherwise.


File::getDataUri

Description

public getDataUri (string $uri, bool $base_64_encode, string|null $mimetype)

Converts a file URL into a data URI.

Parameters

  • (string) $uri : The file URI.
  • (bool) $base_64_encode : TRUE to return the data URI as base-64 encoded content.
  • (string|null) $mimetype : The optional mime type to provide for the data URI. If not provided
    the mime type guesser service will be used.

Return Values

string

The image data URI for use in a src attribute.

Throws Exceptions

\Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException

If the file cannot be read.