File.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  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
  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
  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. try {
  86. $renamed = rename($this->getPathname(), $target);
  87. } finally {
  88. restore_error_handler();
  89. }
  90. if (!$renamed) {
  91. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
  92. }
  93. @chmod($target, 0666 & ~umask());
  94. return $target;
  95. }
  96. public function getContent(): string
  97. {
  98. $content = file_get_contents($this->getPathname());
  99. if (false === $content) {
  100. throw new FileException(sprintf('Could not get the content of the file "%s".', $this->getPathname()));
  101. }
  102. return $content;
  103. }
  104. /**
  105. * @return self
  106. */
  107. protected function getTargetFile(string $directory, string $name = null)
  108. {
  109. if (!is_dir($directory)) {
  110. if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
  111. throw new FileException(sprintf('Unable to create the "%s" directory.', $directory));
  112. }
  113. } elseif (!is_writable($directory)) {
  114. throw new FileException(sprintf('Unable to write in the "%s" directory.', $directory));
  115. }
  116. $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  117. return new self($target, false);
  118. }
  119. /**
  120. * Returns locale independent base name of the given path.
  121. *
  122. * @return string
  123. */
  124. protected function getName(string $name)
  125. {
  126. $originalName = str_replace('\\', '/', $name);
  127. $pos = strrpos($originalName, '/');
  128. $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
  129. return $originalName;
  130. }
  131. }