[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/File/ -> File.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpFoundation\File;
  13  
  14  use Symfony\Component\HttpFoundation\File\Exception\FileException;
  15  use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  16  use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  17  use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
  18  
  19  /**
  20   * A file in the file system.
  21   *
  22   * @author Bernhard Schussek <bschussek@gmail.com>
  23   */
  24  class File extends \SplFileInfo
  25  {
  26      /**
  27       * Constructs a new file from the given path.
  28       *
  29       * @param string $path      The path to the file
  30       * @param bool   $checkPath Whether to check the path or not
  31       *
  32       * @throws FileNotFoundException If the given path is not a file
  33       */
  34      public function __construct($path, $checkPath = true)
  35      {
  36          if ($checkPath && !is_file($path)) {
  37              throw new FileNotFoundException($path);
  38          }
  39  
  40          parent::__construct($path);
  41      }
  42  
  43      /**
  44       * Returns the extension based on the mime type.
  45       *
  46       * If the mime type is unknown, returns null.
  47       *
  48       * This method uses the mime type as guessed by getMimeType()
  49       * to guess the file extension.
  50       *
  51       * @return string|null The guessed extension or null if it cannot be guessed
  52       *
  53       * @see ExtensionGuesser
  54       * @see getMimeType()
  55       */
  56      public function guessExtension()
  57      {
  58          $type = $this->getMimeType();
  59          $guesser = ExtensionGuesser::getInstance();
  60  
  61          return $guesser->guess($type);
  62      }
  63  
  64      /**
  65       * Returns the mime type of the file.
  66       *
  67       * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(),
  68       * mime_content_type() and the system binary "file" (in this order), depending on
  69       * which of those are available.
  70       *
  71       * @return string|null The guessed mime type (i.e. "application/pdf")
  72       *
  73       * @see MimeTypeGuesser
  74       */
  75      public function getMimeType()
  76      {
  77          $guesser = MimeTypeGuesser::getInstance();
  78  
  79          return $guesser->guess($this->getPathname());
  80      }
  81  
  82      /**
  83       * Returns the extension of the file.
  84       *
  85       * \SplFileInfo::getExtension() is not available before PHP 5.3.6
  86       *
  87       * @return string The extension
  88       */
  89      public function getExtension()
  90      {
  91          return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
  92      }
  93  
  94      /**
  95       * Moves the file to a new location.
  96       *
  97       * @param string $directory The destination folder
  98       * @param string $name      The new file name
  99       *
 100       * @return File A File object representing the new file
 101       *
 102       * @throws FileException if the target file could not be created
 103       */
 104      public function move($directory, $name = null)
 105      {
 106          $target = $this->getTargetFile($directory, $name);
 107  
 108          if (!@rename($this->getPathname(), $target)) {
 109              $error = error_get_last();
 110              throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
 111          }
 112  
 113          @chmod($target, 0666 & ~umask());
 114  
 115          return $target;
 116      }
 117  
 118      protected function getTargetFile($directory, $name = null)
 119      {
 120          if (!is_dir($directory)) {
 121              if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
 122                  throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
 123              }
 124          } elseif (!is_writable($directory)) {
 125              throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
 126          }
 127  
 128          $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
 129  
 130          return new self($target, false);
 131      }
 132  
 133      /**
 134       * Returns locale independent base name of the given path.
 135       *
 136       * @param string $name The new file name
 137       *
 138       * @return string containing
 139       */
 140      protected function getName($name)
 141      {
 142          $originalName = str_replace('\\', '/', $name);
 143          $pos = strrpos($originalName, '/');
 144          $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
 145  
 146          return $originalName;
 147      }
 148  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1