Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation\File; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\File\Exception\FileException; Chris@0: use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; Chris@17: use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser; Chris@0: use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; Chris@0: Chris@0: /** Chris@0: * A file in the file system. Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class File extends \SplFileInfo Chris@0: { Chris@0: /** Chris@0: * Constructs a new file from the given path. Chris@0: * Chris@0: * @param string $path The path to the file Chris@0: * @param bool $checkPath Whether to check the path or not Chris@0: * Chris@0: * @throws FileNotFoundException If the given path is not a file Chris@0: */ Chris@0: public function __construct($path, $checkPath = true) Chris@0: { Chris@0: if ($checkPath && !is_file($path)) { Chris@0: throw new FileNotFoundException($path); Chris@0: } Chris@0: Chris@0: parent::__construct($path); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the extension based on the mime type. Chris@0: * Chris@0: * If the mime type is unknown, returns null. Chris@0: * Chris@0: * This method uses the mime type as guessed by getMimeType() Chris@0: * to guess the file extension. Chris@0: * Chris@0: * @return string|null The guessed extension or null if it cannot be guessed Chris@0: * Chris@0: * @see ExtensionGuesser Chris@0: * @see getMimeType() Chris@0: */ Chris@0: public function guessExtension() Chris@0: { Chris@0: $type = $this->getMimeType(); Chris@0: $guesser = ExtensionGuesser::getInstance(); Chris@0: Chris@0: return $guesser->guess($type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the mime type of the file. Chris@0: * Chris@0: * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(), Chris@0: * mime_content_type() and the system binary "file" (in this order), depending on Chris@0: * which of those are available. Chris@0: * Chris@0: * @return string|null The guessed mime type (e.g. "application/pdf") Chris@0: * Chris@0: * @see MimeTypeGuesser Chris@0: */ Chris@0: public function getMimeType() Chris@0: { Chris@0: $guesser = MimeTypeGuesser::getInstance(); Chris@0: Chris@0: return $guesser->guess($this->getPathname()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Moves the file to a new location. Chris@0: * Chris@0: * @param string $directory The destination folder Chris@0: * @param string $name The new file name Chris@0: * Chris@0: * @return self A File object representing the new file Chris@0: * Chris@0: * @throws FileException if the target file could not be created Chris@0: */ Chris@0: public function move($directory, $name = null) Chris@0: { Chris@0: $target = $this->getTargetFile($directory, $name); Chris@0: Chris@16: set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; }); Chris@16: $renamed = rename($this->getPathname(), $target); Chris@16: restore_error_handler(); Chris@16: if (!$renamed) { Chris@16: throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error))); Chris@0: } Chris@0: Chris@0: @chmod($target, 0666 & ~umask()); Chris@0: Chris@0: return $target; Chris@0: } Chris@0: Chris@0: protected function getTargetFile($directory, $name = null) Chris@0: { Chris@0: if (!is_dir($directory)) { Chris@0: if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) { Chris@0: throw new FileException(sprintf('Unable to create the "%s" directory', $directory)); Chris@0: } Chris@0: } elseif (!is_writable($directory)) { Chris@0: throw new FileException(sprintf('Unable to write in the "%s" directory', $directory)); Chris@0: } Chris@0: Chris@17: $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name)); Chris@0: Chris@0: return new self($target, false); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns locale independent base name of the given path. Chris@0: * Chris@0: * @param string $name The new file name Chris@0: * Chris@0: * @return string containing Chris@0: */ Chris@0: protected function getName($name) Chris@0: { Chris@0: $originalName = str_replace('\\', '/', $name); Chris@0: $pos = strrpos($originalName, '/'); Chris@0: $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1); Chris@0: Chris@0: return $originalName; Chris@0: } Chris@0: }