菜谱项目

File.php 4.0KB

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