You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.5 KiB

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