4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\File;
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;
20 * A file in the file system.
22 * @author Bernhard Schussek <bschussek@gmail.com>
24 class File extends \SplFileInfo
27 * Constructs a new file from the given path.
29 * @param string $path The path to the file
30 * @param bool $checkPath Whether to check the path or not
32 * @throws FileNotFoundException If the given path is not a file
34 public function __construct($path, $checkPath = true)
36 if ($checkPath && !is_file($path)) {
37 throw new FileNotFoundException($path);
40 parent::__construct($path);
44 * Returns the extension based on the mime type.
46 * If the mime type is unknown, returns null.
48 * This method uses the mime type as guessed by getMimeType()
49 * to guess the file extension.
51 * @return string|null The guessed extension or null if it cannot be guessed
53 * @see ExtensionGuesser
56 public function guessExtension()
58 $type = $this->getMimeType();
59 $guesser = ExtensionGuesser::getInstance();
61 return $guesser->guess($type);
65 * Returns the mime type of the file.
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.
71 * @return string|null The guessed mime type (e.g. "application/pdf")
73 * @see MimeTypeGuesser
75 public function getMimeType()
77 $guesser = MimeTypeGuesser::getInstance();
79 return $guesser->guess($this->getPathname());
83 * Moves the file to a new location.
85 * @param string $directory The destination folder
86 * @param string $name The new file name
88 * @return self A File object representing the new file
90 * @throws FileException if the target file could not be created
92 public function move($directory, $name = null)
94 $target = $this->getTargetFile($directory, $name);
96 if (!@rename($this->getPathname(), $target)) {
97 $error = error_get_last();
98 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
101 @chmod($target, 0666 & ~umask());
106 protected function getTargetFile($directory, $name = null)
108 if (!is_dir($directory)) {
109 if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
110 throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
112 } elseif (!is_writable($directory)) {
113 throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
116 $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
118 return new self($target, false);
122 * Returns locale independent base name of the given path.
124 * @param string $name The new file name
126 * @return string containing
128 protected function getName($name)
130 $originalName = str_replace('\\', '/', $name);
131 $pos = strrpos($originalName, '/');
132 $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
134 return $originalName;