/vendor/symfony/http-foundation/File/File.php

https://gitlab.com/judielsm/Handora · PHP · 146 lines · 56 code · 18 blank · 72 comment · 6 complexity · df92043bfca151fa3ded8ced1e77e6eb MD5 · raw file

  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\HttpFoundation\File\MimeType\MimeTypeGuesser;
  14. use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
  15. /**
  16. * A file in the file system.
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. *
  20. * @api
  21. */
  22. class File extends \SplFileInfo
  23. {
  24. /**
  25. * Constructs a new file from the given path.
  26. *
  27. * @param string $path The path to the file
  28. * @param bool $checkPath Whether to check the path or not
  29. *
  30. * @throws FileNotFoundException If the given path is not a file
  31. *
  32. * @api
  33. */
  34. public function __construct($path, $checkPath = true)
  35. {
  36. if ($checkPath && !is_file($path)) {
  37. throw new FileNotFoundException($path);
  38. }
  39. parent::__construct($path);
  40. }
  41. /**
  42. * Returns the extension based on the mime type.
  43. *
  44. * If the mime type is unknown, returns null.
  45. *
  46. * This method uses the mime type as guessed by getMimeType()
  47. * to guess the file extension.
  48. *
  49. * @return string|null The guessed extension or null if it cannot be guessed
  50. *
  51. * @api
  52. *
  53. * @see ExtensionGuesser
  54. * @see getMimeType()
  55. */
  56. public function guessExtension()
  57. {
  58. $type = $this->getMimeType();
  59. $guesser = ExtensionGuesser::getInstance();
  60. return $guesser->guess($type);
  61. }
  62. /**
  63. * Returns the mime type of the file.
  64. *
  65. * The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(),
  66. * mime_content_type() and the system binary "file" (in this order), depending on
  67. * which of those are available.
  68. *
  69. * @return string|null The guessed mime type (i.e. "application/pdf")
  70. *
  71. * @see MimeTypeGuesser
  72. *
  73. * @api
  74. */
  75. public function getMimeType()
  76. {
  77. $guesser = MimeTypeGuesser::getInstance();
  78. return $guesser->guess($this->getPathname());
  79. }
  80. /**
  81. * Moves the file to a new location.
  82. *
  83. * @param string $directory The destination folder
  84. * @param string $name The new file name
  85. *
  86. * @return File A File object representing the new file
  87. *
  88. * @throws FileException if the target file could not be created
  89. *
  90. * @api
  91. */
  92. public function move($directory, $name = null)
  93. {
  94. $target = $this->getTargetFile($directory, $name);
  95. if (!@rename($this->getPathname(), $target)) {
  96. $error = error_get_last();
  97. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
  98. }
  99. @chmod($target, 0666 & ~umask());
  100. return $target;
  101. }
  102. protected function getTargetFile($directory, $name = null)
  103. {
  104. if (!is_dir($directory)) {
  105. if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
  106. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  107. }
  108. } elseif (!is_writable($directory)) {
  109. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  110. }
  111. $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  112. return new self($target, false);
  113. }
  114. /**
  115. * Returns locale independent base name of the given path.
  116. *
  117. * @param string $name The new file name
  118. *
  119. * @return string containing
  120. */
  121. protected function getName($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. }