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/validator/Constraints/File.php \Symfony\Component\Validator\Constraints\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

A file in the file system.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

  • class \Symfony\Component\HttpFoundation\File\File extends \Symfony\Component\HttpFoundation\File\SplFileInfo

Expanded class hierarchy of File

4 files declare their use of File
BinaryFileResponse.php in vendor/symfony/http-foundation/BinaryFileResponse.php
DataUriNormalizer.php in vendor/symfony/serializer/Normalizer/DataUriNormalizer.php
FileValidator.php in vendor/symfony/validator/Constraints/FileValidator.php
UploadedFile.php in vendor/symfony/psr-http-message-bridge/Factory/UploadedFile.php
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/http-foundation/File/File.php, line 23

Namespace

Symfony\Component\HttpFoundation\File
View source
class File extends \SplFileInfo {
    
    /**
     * Constructs a new file from the given path.
     *
     * @param string $path      The path to the file
     * @param bool   $checkPath Whether to check the path or not
     *
     * @throws FileNotFoundException If the given path is not a file
     */
    public function __construct(string $path, bool $checkPath = true) {
        if ($checkPath && !is_file($path)) {
            throw new FileNotFoundException($path);
        }
        parent::__construct($path);
    }
    
    /**
     * Returns the extension based on the mime type.
     *
     * If the mime type is unknown, returns null.
     *
     * This method uses the mime type as guessed by getMimeType()
     * to guess the file extension.
     *
     * @see MimeTypes
     * @see getMimeType()
     */
    public function guessExtension() : ?string {
        if (!class_exists(MimeTypes::class)) {
            throw new \LogicException('You cannot guess the extension as the Mime component is not installed. Try running "composer require symfony/mime".');
        }
        return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null;
    }
    
    /**
     * Returns the mime type of the file.
     *
     * The mime type is guessed using a MimeTypeGuesserInterface instance,
     * which uses finfo_file() then the "file" system binary,
     * depending on which of those are available.
     *
     * @see MimeTypes
     */
    public function getMimeType() : ?string {
        if (!class_exists(MimeTypes::class)) {
            throw new \LogicException('You cannot guess the mime type as the Mime component is not installed. Try running "composer require symfony/mime".');
        }
        return MimeTypes::getDefault()->guessMimeType($this->getPathname());
    }
    
    /**
     * Moves the file to a new location.
     *
     * @throws FileException if the target file could not be created
     */
    public function move(string $directory, ?string $name = null) : self {
        $target = $this->getTargetFile($directory, $name);
        set_error_handler(function ($type, $msg) use (&$error) {
            $error = $msg;
        });
        try {
            $renamed = rename($this->getPathname(), $target);
        } finally {
            restore_error_handler();
        }
        if (!$renamed) {
            throw new FileException(\sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
        }
        @chmod($target, 0666 & ~umask());
        return $target;
    }
    public function getContent() : string {
        $content = file_get_contents($this->getPathname());
        if (false === $content) {
            throw new FileException(\sprintf('Could not get the content of the file "%s".', $this->getPathname()));
        }
        return $content;
    }
    protected function getTargetFile(string $directory, ?string $name = null) : self {
        if (!is_dir($directory)) {
            if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
                throw new FileException(\sprintf('Unable to create the "%s" directory.', $directory));
            }
        }
        elseif (!is_writable($directory)) {
            throw new FileException(\sprintf('Unable to write in the "%s" directory.', $directory));
        }
        $target = rtrim($directory, '/\\') . \DIRECTORY_SEPARATOR . (null === $name ? $this->getBasename() : $this->getName($name));
        return new self($target, false);
    }
    
    /**
     * Returns locale independent base name of the given path.
     */
    protected function getName(string $name) : string {
        $originalName = str_replace('\\', '/', $name);
        $pos = strrpos($originalName, '/');
        return false === $pos ? $originalName : substr($originalName, $pos + 1);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
File::getContent public function
File::getMimeType public function Returns the mime type of the file.
File::getName protected function Returns locale independent base name of the given path.
File::getTargetFile protected function
File::guessExtension public function Returns the extension based on the mime type.
File::move public function Moves the file to a new location. 1
File::__construct public function Constructs a new file from the given path. 1

API Navigation

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