class UploadedFile

A file uploaded through a form.

@author Bernhard Schussek <bschussek@gmail.com> @author Florian Eckerstorfer <florian@eckerstorfer.org> @author Fabien Potencier <fabien@symfony.com>

@api

Hierarchy

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

Expanded class hierarchy of UploadedFile

5 files declare their use of UploadedFile
Client.php in drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php
ClientTest.php in drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/ClientTest.php
FileBag.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/FileBag.php
FileBagTest.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/FileBagTest.php
UploadedFileTest.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/File/UploadedFile.php, line 26

Namespace

Symfony\Component\HttpFoundation\File
View source
class UploadedFile extends File {

  /**
   * Whether the test mode is activated.
   *
   * Local files are used in test mode hence the code should not enforce HTTP uploads.
   *
   * @var Boolean
   */
  private $test = false;

  /**
   * The original name of the uploaded file.
   *
   * @var string
   */
  private $originalName;

  /**
   * The mime type provided by the uploader.
   *
   * @var string
   */
  private $mimeType;

  /**
   * The file size provided by the uploader.
   *
   * @var string
   */
  private $size;

  /**
   * The UPLOAD_ERR_XXX constant provided by the uploader.
   *
   * @var integer
   */
  private $error;

  /**
   * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
   *
   * The file object is only created when the uploaded file is valid (i.e. when the
   * isValid() method returns true). Otherwise the only methods that could be called
   * on an UploadedFile instance are:
   *
   *   * getClientOriginalName,
   *   * getClientMimeType,
   *   * isValid,
   *   * getError.
   *
   * Calling any other method on an non-valid instance will cause an unpredictable result.
   *
   * @param string  $path         The full temporary path to the file
   * @param string  $originalName The original file name
   * @param string  $mimeType     The type of the file as provided by PHP
   * @param integer $size         The file size
   * @param integer $error        The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants)
   * @param Boolean $test         Whether the test mode is active
   *
   * @throws FileException         If file_uploads is disabled
   * @throws FileNotFoundException If the file does not exist
   *
   * @api
   */
  public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false) {
    if (!ini_get('file_uploads')) {
      throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
    }
    $this->originalName = basename($originalName);
    $this->mimeType = $mimeType ?: 'application/octet-stream';
    $this->size = $size;
    $this->error = $error ?: UPLOAD_ERR_OK;
    $this->test = (bool) $test;
    parent::__construct($path, UPLOAD_ERR_OK === $this->error);
  }

  /**
   * Returns the original file name.
   *
   * It is extracted from the request from which the file has been uploaded.
   * Then is should not be considered as a safe value.
   *
   * @return string|null The original name
   *
   * @api
   */
  public function getClientOriginalName() {
    return $this->originalName;
  }

  /**
   * Returns the file mime type.
   *
   * It is extracted from the request from which the file has been uploaded.
   * Then is should not be considered as a safe value.
   *
   * @return string|null The mime type
   *
   * @api
   */
  public function getClientMimeType() {
    return $this->mimeType;
  }

  /**
   * Returns the file size.
   *
   * It is extracted from the request from which the file has been uploaded.
   * Then is should not be considered as a safe value.
   *
   * @return integer|null The file size
   *
   * @api
   */
  public function getClientSize() {
    return $this->size;
  }

  /**
   * Returns the upload error.
   *
   * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
   * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
   *
   * @return integer The upload error
   *
   * @api
   */
  public function getError() {
    return $this->error;
  }

  /**
   * Returns whether the file was uploaded successfully.
   *
   * @return Boolean  True if no error occurred during uploading
   *
   * @api
   */
  public function isValid() {
    return $this->error === UPLOAD_ERR_OK;
  }

  /**
   * Moves the file to a new location.
   *
   * @param string $directory The destination folder
   * @param string $name      The new file name
   *
   * @return File A File object representing the new file
   *
   * @throws FileException if the file has not been uploaded via Http
   *
   * @api
   */
  public function move($directory, $name = null) {
    if ($this
      ->isValid() && ($this->test || is_uploaded_file($this
      ->getPathname()))) {
      return parent::move($directory, $name);
    }
    throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this
      ->getPathname()));
  }

  /**
   * Returns the maximum size of an uploaded file as configured in php.ini
   *
   * @return int The maximum size of an uploaded file in bytes
   */
  public static function getMaxFilesize() {
    $max = trim(ini_get('upload_max_filesize'));
    if ('' === $max) {
      return PHP_INT_MAX;
    }
    switch (strtolower(substr($max, -1))) {
      case 'g':
        $max *= 1024;
      case 'm':
        $max *= 1024;
      case 'k':
        $max *= 1024;
    }
    return (int) $max;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
File::getExtension public function Returns the extension of the file.
File::getMimeType public function Returns the mime type of the file.
File::guessExtension public function Returns the extension based on the mime type.
UploadedFile::$error private property The UPLOAD_ERR_XXX constant provided by the uploader.
UploadedFile::$mimeType private property The mime type provided by the uploader.
UploadedFile::$originalName private property The original name of the uploaded file.
UploadedFile::$size private property The file size provided by the uploader.
UploadedFile::$test private property Whether the test mode is activated.
UploadedFile::getClientMimeType public function Returns the file mime type.
UploadedFile::getClientOriginalName public function Returns the original file name.
UploadedFile::getClientSize public function Returns the file size.
UploadedFile::getError public function Returns the upload error.
UploadedFile::getMaxFilesize public static function Returns the maximum size of an uploaded file as configured in php.ini
UploadedFile::isValid public function Returns whether the file was uploaded successfully.
UploadedFile::move public function Moves the file to a new location. Overrides File::move
UploadedFile::__construct public function Accepts the information of the uploaded file as provided by the PHP global $_FILES. Overrides File::__construct