123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace Symfony\Component\HttpFoundation\File;
- use Symfony\Component\HttpFoundation\File\Exception\FileException;
- use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
- use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
- use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
- class File extends \SplFileInfo
- {
-
- public function __construct($path, $checkPath = true)
- {
- if ($checkPath && !is_file($path)) {
- throw new FileNotFoundException($path);
- }
- parent::__construct($path);
- }
-
- public function guessExtension()
- {
- $type = $this->getMimeType();
- $guesser = ExtensionGuesser::getInstance();
- return $guesser->guess($type);
- }
-
- public function getMimeType()
- {
- $guesser = MimeTypeGuesser::getInstance();
- return $guesser->guess($this->getPathname());
- }
-
- public function getExtension()
- {
- return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
- }
-
- public function move($directory, $name = null)
- {
- if (!is_dir($directory)) {
- if (false === @mkdir($directory, 0777, true)) {
- throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
- }
- } elseif (!is_writable($directory)) {
- throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
- }
- $target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : basename($name));
- if (!@rename($this->getPathname(), $target)) {
- $error = error_get_last();
- throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
- }
- chmod($target, 0666);
- return new File($target);
- }
- }
|