Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. File.php

class File

Same name in this branch
  1. 11.1.x vendor/phpunit/php-code-coverage/src/Node/File.php \SebastianBergmann\CodeCoverage\Node\File
  2. 11.1.x vendor/phpunit/php-code-coverage/src/Report/Html/Renderer/File.php \SebastianBergmann\CodeCoverage\Report\Html\File
  3. 11.1.x vendor/phpunit/php-code-coverage/src/Report/Xml/File.php \SebastianBergmann\CodeCoverage\Report\Xml\File
  4. 11.1.x vendor/phpunit/phpunit/src/TextUI/Configuration/Value/File.php \PHPUnit\TextUI\Configuration\File
  5. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/File.php \PhpParser\Node\Scalar\MagicConst\File
  6. 11.1.x vendor/symfony/mime/Part/File.php \Symfony\Component\Mime\Part\File
  7. 11.1.x vendor/symfony/http-foundation/File/File.php \Symfony\Component\HttpFoundation\File\File
  8. 11.1.x vendor/squizlabs/php_codesniffer/src/Files/File.php \PHP_CodeSniffer\Files\File
  9. 11.1.x core/lib/Drupal/Core/Render/Element/File.php \Drupal\Core\Render\Element\File
  10. 11.1.x core/modules/file/src/Plugin/migrate/source/d6/File.php \Drupal\file\Plugin\migrate\source\d6\File
  11. 11.1.x core/modules/file/src/Plugin/migrate/source/d7/File.php \Drupal\file\Plugin\migrate\source\d7\File
  12. 11.1.x core/modules/file/src/Plugin/views/field/File.php \Drupal\file\Plugin\views\field\File
  13. 11.1.x core/modules/file/src/Plugin/views/wizard/File.php \Drupal\file\Plugin\views\wizard\File
  14. 11.1.x core/modules/file/src/Entity/File.php \Drupal\file\Entity\File
  15. 11.1.x core/modules/media/src/Plugin/media/Source/File.php \Drupal\media\Plugin\media\Source\File

Validates that a value is a valid "file".

A file can be one of the following:

  • A string (or object with a __toString() method) path to an existing file;
  • A valid {

@property int $maxSize

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\Constraint
    • class \Symfony\Component\Validator\Constraints\File extends \Symfony\Component\Validator\Constraint

Expanded class hierarchy of File

See also

\Symfony\Component\HttpFoundation\File\File File} object (including objects of {@see \Symfony\Component\HttpFoundation\File\UploadedFile UploadedFile} class).

171 string references to 'File'
AbstractUriElement::__construct in vendor/symfony/dom-crawler/AbstractUriElement.php
ArchiveCommand::execute in vendor/composer/composer/src/Composer/Command/ArchiveCommand.php
Executes the current command.
AssetResolver::getCssAssets in core/lib/Drupal/Core/Asset/AssetResolver.php
Returns the CSS assets for the current response's libraries.
AssetResolver::getJsAssets in core/lib/Drupal/Core/Asset/AssetResolver.php
Returns the JavaScript assets for the current response's libraries.
BaseFieldFileFormatterBase::isApplicable in core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php
Returns if the formatter can be used for the provided field.

... See full list

File

vendor/symfony/validator/Constraints/File.php, line 28

Namespace

Symfony\Component\Validator\Constraints
View source
class File extends Constraint {
    // Check the Image constraint for clashes if adding new constants here
    public const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
    public const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
    public const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
    public const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
    public const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
    public const INVALID_EXTENSION_ERROR = 'c8c7315c-6186-4719-8b71-5659e16bdcb7';
    public const FILENAME_TOO_LONG = 'e5706483-91a8-49d8-9a59-5e81a3c634a8';
    protected const ERROR_NAMES = [
        self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',
        self::NOT_READABLE_ERROR => 'NOT_READABLE_ERROR',
        self::EMPTY_ERROR => 'EMPTY_ERROR',
        self::TOO_LARGE_ERROR => 'TOO_LARGE_ERROR',
        self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
        self::INVALID_EXTENSION_ERROR => 'INVALID_EXTENSION_ERROR',
        self::FILENAME_TOO_LONG => 'FILENAME_TOO_LONG',
    ];
    public ?bool $binaryFormat = null;
    public array|string $mimeTypes = [];
    public ?int $filenameMaxLength = null;
    public array|string $extensions = [];
    public string $notFoundMessage = 'The file could not be found.';
    public string $notReadableMessage = 'The file is not readable.';
    public string $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
    public string $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
    public string $extensionsMessage = 'The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}.';
    public string $disallowEmptyMessage = 'An empty file is not allowed.';
    public string $filenameTooLongMessage = 'The filename is too long. It should have {{ filename_max_length }} character or less.|The filename is too long. It should have {{ filename_max_length }} characters or less.';
    public string $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.';
    public string $uploadFormSizeErrorMessage = 'The file is too large.';
    public string $uploadPartialErrorMessage = 'The file was only partially uploaded.';
    public string $uploadNoFileErrorMessage = 'No file was uploaded.';
    public string $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini.';
    public string $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk.';
    public string $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
    public string $uploadErrorMessage = 'The file could not be uploaded.';
    protected int|string|null $maxSize = null;
    
    /**
     * @param array<string,mixed>|null           $options
     * @param positive-int|string|null           $maxSize                     The max size of the underlying file
     * @param bool|null                          $binaryFormat                Pass true to use binary-prefixed units (KiB, MiB, etc.) or false to use SI-prefixed units (kB, MB) in displayed messages. Pass null to guess the format from the maxSize option. (defaults to null)
     * @param string[]|string|null               $mimeTypes                   Acceptable media type(s). Prefer the extensions option that also enforce the file's extension consistency.
     * @param positive-int|null                  $filenameMaxLength           Maximum length of the file name
     * @param string|null                        $disallowEmptyMessage        Enable empty upload validation with this message in case of error
     * @param string|null                        $uploadIniSizeErrorMessage   Message if the file size exceeds the max size configured in php.ini
     * @param string|null                        $uploadFormSizeErrorMessage  Message if the file size exceeds the max size configured in the HTML input field
     * @param string|null                        $uploadPartialErrorMessage   Message if the file is only partially uploaded
     * @param string|null                        $uploadNoTmpDirErrorMessage  Message if there is no upload_tmp_dir in php.ini
     * @param string|null                        $uploadCantWriteErrorMessage Message if the uploaded file can not be stored in the temporary directory
     * @param string|null                        $uploadErrorMessage          Message if an unknown error occurred on upload
     * @param string[]|null                      $groups
     * @param array<string|string[]>|string|null $extensions                  A list of valid extensions to check. Related media types are also enforced ({@see https://symfony.com/doc/current/reference/constraints/File.html#extensions})
     *
     * @see https://www.iana.org/assignments/media-types/media-types.xhtml Existing media types
     */
    public function __construct(?array $options = null, int|string|null $maxSize = null, ?bool $binaryFormat = null, array|string|null $mimeTypes = null, ?int $filenameMaxLength = null, ?string $notFoundMessage = null, ?string $notReadableMessage = null, ?string $maxSizeMessage = null, ?string $mimeTypesMessage = null, ?string $disallowEmptyMessage = null, ?string $filenameTooLongMessage = null, ?string $uploadIniSizeErrorMessage = null, ?string $uploadFormSizeErrorMessage = null, ?string $uploadPartialErrorMessage = null, ?string $uploadNoFileErrorMessage = null, ?string $uploadNoTmpDirErrorMessage = null, ?string $uploadCantWriteErrorMessage = null, ?string $uploadExtensionErrorMessage = null, ?string $uploadErrorMessage = null, ?array $groups = null, mixed $payload = null, array|string|null $extensions = null, ?string $extensionsMessage = null) {
        parent::__construct($options, $groups, $payload);
        $this->maxSize = $maxSize ?? $this->maxSize;
        $this->binaryFormat = $binaryFormat ?? $this->binaryFormat;
        $this->mimeTypes = $mimeTypes ?? $this->mimeTypes;
        $this->filenameMaxLength = $filenameMaxLength ?? $this->filenameMaxLength;
        $this->extensions = $extensions ?? $this->extensions;
        $this->notFoundMessage = $notFoundMessage ?? $this->notFoundMessage;
        $this->notReadableMessage = $notReadableMessage ?? $this->notReadableMessage;
        $this->maxSizeMessage = $maxSizeMessage ?? $this->maxSizeMessage;
        $this->mimeTypesMessage = $mimeTypesMessage ?? $this->mimeTypesMessage;
        $this->extensionsMessage = $extensionsMessage ?? $this->extensionsMessage;
        $this->disallowEmptyMessage = $disallowEmptyMessage ?? $this->disallowEmptyMessage;
        $this->filenameTooLongMessage = $filenameTooLongMessage ?? $this->filenameTooLongMessage;
        $this->uploadIniSizeErrorMessage = $uploadIniSizeErrorMessage ?? $this->uploadIniSizeErrorMessage;
        $this->uploadFormSizeErrorMessage = $uploadFormSizeErrorMessage ?? $this->uploadFormSizeErrorMessage;
        $this->uploadPartialErrorMessage = $uploadPartialErrorMessage ?? $this->uploadPartialErrorMessage;
        $this->uploadNoFileErrorMessage = $uploadNoFileErrorMessage ?? $this->uploadNoFileErrorMessage;
        $this->uploadNoTmpDirErrorMessage = $uploadNoTmpDirErrorMessage ?? $this->uploadNoTmpDirErrorMessage;
        $this->uploadCantWriteErrorMessage = $uploadCantWriteErrorMessage ?? $this->uploadCantWriteErrorMessage;
        $this->uploadExtensionErrorMessage = $uploadExtensionErrorMessage ?? $this->uploadExtensionErrorMessage;
        $this->uploadErrorMessage = $uploadErrorMessage ?? $this->uploadErrorMessage;
        if (null !== $this->maxSize) {
            $this->normalizeBinaryFormat($this->maxSize);
        }
    }
    public function __set(string $option, mixed $value) : void {
        if ('maxSize' === $option) {
            $this->normalizeBinaryFormat($value);
            return;
        }
        parent::__set($option, $value);
    }
    public function __get(string $option) : mixed {
        if ('maxSize' === $option) {
            return $this->maxSize;
        }
        return parent::__get($option);
    }
    public function __isset(string $option) : bool {
        if ('maxSize' === $option) {
            return true;
        }
        return parent::__isset($option);
    }
    private function normalizeBinaryFormat(int|string $maxSize) : void {
        $factors = [
            'k' => 1000,
            'ki' => 1 << 10,
            'm' => 1000 * 1000,
            'mi' => 1 << 20,
            'g' => 1000 * 1000 * 1000,
            'gi' => 1 << 30,
        ];
        if (ctype_digit((string) $maxSize)) {
            $this->maxSize = (int) $maxSize;
            $this->binaryFormat ??= false;
        }
        elseif (preg_match('/^(\\d++)(' . implode('|', array_keys($factors)) . ')$/i', $maxSize, $matches)) {
            $this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
            $this->binaryFormat ??= 2 === \strlen($unit);
        }
        else {
            throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum size.', $maxSize));
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::$groups public property The groups that the constraint belongs to.
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::addImplicitGroupName public function Adds the given group if this constraint is in the Default group. 2
Constraint::CLASS_CONSTRAINT public constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP public constant The name of the group given to all constraints with no explicit group.
Constraint::getDefaultOption public function Returns the name of the default option. 28
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getRequiredOptions public function Returns the name of the required options. 22
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or
both.
8
Constraint::normalizeOptions protected function
Constraint::PROPERTY_CONSTRAINT public constant Marks a constraint that can be put onto properties.
Constraint::validatedBy public function Returns the name of the class that validates this constraint. 9
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.
File::$binaryFormat public property
File::$disallowEmptyMessage public property
File::$extensions public property
File::$extensionsMessage public property
File::$filenameMaxLength public property
File::$filenameTooLongMessage public property
File::$maxSize protected property
File::$maxSizeMessage public property
File::$mimeTypes public property 1
File::$mimeTypesMessage public property 1
File::$notFoundMessage public property
File::$notReadableMessage public property
File::$uploadCantWriteErrorMessage public property
File::$uploadErrorMessage public property
File::$uploadExtensionErrorMessage public property
File::$uploadFormSizeErrorMessage public property
File::$uploadIniSizeErrorMessage public property
File::$uploadNoFileErrorMessage public property
File::$uploadNoTmpDirErrorMessage public property
File::$uploadPartialErrorMessage public property
File::EMPTY_ERROR public constant
File::ERROR_NAMES protected constant Maps error codes to the names of their constants. Overrides Constraint::ERROR_NAMES 1
File::FILENAME_TOO_LONG public constant
File::INVALID_EXTENSION_ERROR public constant
File::INVALID_MIME_TYPE_ERROR public constant
File::normalizeBinaryFormat private function
File::NOT_FOUND_ERROR public constant
File::NOT_READABLE_ERROR public constant
File::TOO_LARGE_ERROR public constant
File::__construct public function Overrides Constraint::__construct 1
File::__get public function Returns the value of a lazily initialized option. Overrides Constraint::__get
File::__isset public function Overrides Constraint::__isset
File::__set public function Sets the value of a lazily initialized option. Overrides Constraint::__set

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal