File.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null;
  52. }
  53. /**
  54. * Returns the mime type of the file.
  55. *
  56. * The mime type is guessed using a MimeTypeGuesserInterface instance,
  57. * which uses finfo_file() then the "file" system binary,
  58. * depending on which of those are available.
  59. *
  60. * @return string|null The guessed mime type (e.g. "application/pdf")
  61. *
  62. * @see MimeTypes
  63. */
  64. public function getMimeType()
  65. {
  66. return MimeTypes::getDefault()->guessMimeType($this->getPathname());
  67. }
  68. /**
  69. * Moves the file to a new location.
  70. *
  71. * @return self A File object representing the new file
  72. *
  73. * @throws FileException if the target file could not be created
  74. */
  75. public function move(string $directory, string $name = null)
  76. {
  77. $target = $this->getTargetFile($directory, $name);
  78. set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
  79. $renamed = rename($this->getPathname(), $target);
  80. restore_error_handler();
  81. if (!$renamed) {
  82. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
  83. }
  84. @chmod($target, 0666 & ~umask());
  85. return $target;
  86. }
  87. /**
  88. * @return self
  89. */
  90. protected function getTargetFile(string $directory, string $name = null)
  91. {
  92. if (!is_dir($directory)) {
  93. if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
  94. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  95. }
  96. } elseif (!is_writable($directory)) {
  97. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  98. }
  99. $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  100. return new self($target, false);
  101. }
  102. /**
  103. * Returns locale independent base name of the given path.
  104. *
  105. * @return string
  106. */
  107. protected function getName(string $name)
  108. {
  109. $originalName = str_replace('\\', '/', $name);
  110. $pos = strrpos($originalName, '/');
  111. $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
  112. return $originalName;
  113. }
  114. }