File.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @param string $directory The destination folder
  72. * @param string $name The new file name
  73. *
  74. * @return self A File object representing the new file
  75. *
  76. * @throws FileException if the target file could not be created
  77. */
  78. public function move($directory, $name = null)
  79. {
  80. $target = $this->getTargetFile($directory, $name);
  81. set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
  82. $renamed = rename($this->getPathname(), $target);
  83. restore_error_handler();
  84. if (!$renamed) {
  85. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
  86. }
  87. @chmod($target, 0666 & ~umask());
  88. return $target;
  89. }
  90. /**
  91. * @return self
  92. */
  93. protected function getTargetFile($directory, $name = null)
  94. {
  95. if (!is_dir($directory)) {
  96. if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
  97. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  98. }
  99. } elseif (!is_writable($directory)) {
  100. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  101. }
  102. $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  103. return new self($target, false);
  104. }
  105. /**
  106. * Returns locale independent base name of the given path.
  107. *
  108. * @param string $name The new file name
  109. *
  110. * @return string
  111. */
  112. protected function getName($name)
  113. {
  114. $originalName = str_replace('\\', '/', $name);
  115. $pos = strrpos($originalName, '/');
  116. $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
  117. return $originalName;
  118. }
  119. }