File.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Boolean $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. * Returns the extension of the file.
  82. *
  83. * \SplFileInfo::getExtension() is not available before PHP 5.3.6
  84. *
  85. * @return string The extension
  86. *
  87. * @api
  88. */
  89. public function getExtension()
  90. {
  91. return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
  92. }
  93. /**
  94. * Moves the file to a new location.
  95. *
  96. * @param string $directory The destination folder
  97. * @param string $name The new file name
  98. *
  99. * @return File A File object representing the new file
  100. *
  101. * @throws FileException if the target file could not be created
  102. *
  103. * @api
  104. */
  105. public function move($directory, $name = null)
  106. {
  107. $target = $this->getTargetFile($directory, $name);
  108. if (!@rename($this->getPathname(), $target)) {
  109. $error = error_get_last();
  110. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
  111. }
  112. @chmod($target, 0666 & ~umask());
  113. return $target;
  114. }
  115. protected function getTargetFile($directory, $name = null)
  116. {
  117. if (!is_dir($directory)) {
  118. if (false === @mkdir($directory, 0777, true)) {
  119. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  120. }
  121. } elseif (!is_writable($directory)) {
  122. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  123. }
  124. $target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  125. return new File($target, false);
  126. }
  127. /**
  128. * Returns locale independent base name of the given path.
  129. *
  130. * @param string $name The new file name
  131. *
  132. * @return string containing
  133. */
  134. protected function getName($name)
  135. {
  136. $originalName = str_replace('\\', '/', $name);
  137. $pos = strrpos($originalName, '/');
  138. $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
  139. return $originalName;
  140. }
  141. }