Overview

Namespaces

  • Composer
    • Autoload
  • Guzzle
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
        • Header
      • QueryAggregator
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Mock
    • Stream
  • Mockery
    • Adapter
      • Phpunit
    • CountValidator
    • Exception
    • Generator
      • StringManipulation
        • Pass
    • Loader
    • Matcher
  • None
  • Omnipay
    • Common
      • Exception
      • Message
    • Dummy
      • Message
    • Fatzebra
      • Message
  • PHP
  • Symfony
    • Component
      • EventDispatcher
        • Debug
        • DependencyInjection
        • Tests
          • Debug
          • DependencyInjection
      • HttpFoundation
        • File
          • Exception
          • MimeType
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
        • Tests
          • File
            • MimeType
          • Session
            • Attribute
            • Flash
            • Storage
              • Handler
              • Proxy
      • Yaml
        • Exception
        • Tests

Classes

  • Symfony\Component\Yaml\Tests\A
  • Symfony\Component\Yaml\Tests\B
  • Symfony\Component\Yaml\Tests\DumperTest
  • Symfony\Component\Yaml\Tests\InlineTest
  • Symfony\Component\Yaml\Tests\ParseExceptionTest
  • Symfony\Component\Yaml\Tests\ParserTest
  • Symfony\Component\Yaml\Tests\YamlTest
  • Overview
  • Namespace
  • Function
  • Tree
  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:  * @api
 25:  */
 26: class File extends \SplFileInfo
 27: {
 28:     /**
 29:      * Constructs a new file from the given path.
 30:      *
 31:      * @param string $path      The path to the file
 32:      * @param bool   $checkPath Whether to check the path or not
 33:      *
 34:      * @throws FileNotFoundException If the given path is not a file
 35:      *
 36:      * @api
 37:      */
 38:     public function __construct($path, $checkPath = true)
 39:     {
 40:         if ($checkPath && !is_file($path)) {
 41:             throw new FileNotFoundException($path);
 42:         }
 43: 
 44:         parent::__construct($path);
 45:     }
 46: 
 47:     /**
 48:      * Returns the extension based on the mime type.
 49:      *
 50:      * If the mime type is unknown, returns null.
 51:      *
 52:      * This method uses the mime type as guessed by getMimeType()
 53:      * to guess the file extension.
 54:      *
 55:      * @return string|null The guessed extension or null if it cannot be guessed
 56:      *
 57:      * @api
 58:      *
 59:      * @see ExtensionGuesser
 60:      * @see getMimeType()
 61:      */
 62:     public function guessExtension()
 63:     {
 64:         $type = $this->getMimeType();
 65:         $guesser = ExtensionGuesser::getInstance();
 66: 
 67:         return $guesser->guess($type);
 68:     }
 69: 
 70:     /**
 71:      * Returns the mime type of the file.
 72:      *
 73:      * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(),
 74:      * mime_content_type() and the system binary "file" (in this order), depending on
 75:      * which of those are available.
 76:      *
 77:      * @return string|null The guessed mime type (i.e. "application/pdf")
 78:      *
 79:      * @see MimeTypeGuesser
 80:      *
 81:      * @api
 82:      */
 83:     public function getMimeType()
 84:     {
 85:         $guesser = MimeTypeGuesser::getInstance();
 86: 
 87:         return $guesser->guess($this->getPathname());
 88:     }
 89: 
 90:     /**
 91:      * Returns the extension of the file.
 92:      *
 93:      * \SplFileInfo::getExtension() is not available before PHP 5.3.6
 94:      *
 95:      * @return string The extension
 96:      *
 97:      * @api
 98:      */
 99:     public function getExtension()
100:     {
101:         return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
102:     }
103: 
104:     /**
105:      * Moves the file to a new location.
106:      *
107:      * @param string $directory The destination folder
108:      * @param string $name      The new file name
109:      *
110:      * @return File A File object representing the new file
111:      *
112:      * @throws FileException if the target file could not be created
113:      *
114:      * @api
115:      */
116:     public function move($directory, $name = null)
117:     {
118:         $target = $this->getTargetFile($directory, $name);
119: 
120:         if (!@rename($this->getPathname(), $target)) {
121:             $error = error_get_last();
122:             throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
123:         }
124: 
125:         @chmod($target, 0666 & ~umask());
126: 
127:         return $target;
128:     }
129: 
130:     protected function getTargetFile($directory, $name = null)
131:     {
132:         if (!is_dir($directory)) {
133:             if (false === @mkdir($directory, 0777, true)) {
134:                 throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
135:             }
136:         } elseif (!is_writable($directory)) {
137:             throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
138:         }
139: 
140:         $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
141: 
142:         return new File($target, false);
143:     }
144: 
145:     /**
146:      * Returns locale independent base name of the given path.
147:      *
148:      * @param string $name The new file name
149:      *
150:      * @return string containing
151:      */
152:     protected function getName($name)
153:     {
154:         $originalName = str_replace('\\', '/', $name);
155:         $pos = strrpos($originalName, '/');
156:         $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
157: 
158:         return $originalName;
159:     }
160: }
161: 
Omnipay Fat Zebra / Paystream Gateway Module API Documentation API documentation generated by ApiGen