File.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <bernhard.schussek@symfony.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. * @return string|null The guessed extension or null if it cannot be guessed
  47. *
  48. * @api
  49. */
  50. public function guessExtension()
  51. {
  52. $type = $this->getMimeType();
  53. $guesser = ExtensionGuesser::getInstance();
  54. return $guesser->guess($type);
  55. }
  56. /**
  57. * Returns the mime type of the file.
  58. *
  59. * The mime type is guessed using the functions finfo(), mime_content_type()
  60. * and the system binary "file" (in this order), depending on which of those
  61. * is available on the current operating system.
  62. *
  63. * @return string|null The guessed mime type (i.e. "application/pdf")
  64. *
  65. * @api
  66. */
  67. public function getMimeType()
  68. {
  69. $guesser = MimeTypeGuesser::getInstance();
  70. return $guesser->guess($this->getPathname());
  71. }
  72. /**
  73. * Returns the extension of the file.
  74. *
  75. * \SplFileInfo::getExtension() is not available before PHP 5.3.6
  76. *
  77. * @return string The extension
  78. *
  79. * @api
  80. */
  81. public function getExtension()
  82. {
  83. return pathinfo($this->getBasename(), PATHINFO_EXTENSION);
  84. }
  85. /**
  86. * Moves the file to a new location.
  87. *
  88. * @param string $directory The destination folder
  89. * @param string $name The new file name
  90. *
  91. * @return File A File object representing the new file
  92. *
  93. * @throws FileException if the target file could not be created
  94. *
  95. * @api
  96. */
  97. public function move($directory, $name = null)
  98. {
  99. if (!is_dir($directory)) {
  100. if (false === @mkdir($directory, 0777, true)) {
  101. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  102. }
  103. } elseif (!is_writable($directory)) {
  104. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  105. }
  106. $target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : basename($name));
  107. if (!@rename($this->getPathname(), $target)) {
  108. $error = error_get_last();
  109. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
  110. }
  111. chmod($target, 0666);
  112. return new File($target);
  113. }
  114. }