PageRenderTime 48ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/laravel/vendor/Symfony/Component/HttpFoundation/File/File.php

https://bitbucket.org/Maron1/taqman
PHP | 152 lines | 61 code | 18 blank | 73 comment | 5 complexity | dafb8bcf388d7bc648f59bd9e5c6b22d 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 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. $target = $this->getTargetFile($directory, $name);
  100. if (!@rename($this->getPathname(), $target)) {
  101. $error = error_get_last();
  102. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
  103. }
  104. @chmod($target, 0666 & ~umask());
  105. return $target;
  106. }
  107. protected function getTargetFile($directory, $name = null)
  108. {
  109. if (!is_dir($directory)) {
  110. if (false === @mkdir($directory, 0777, true)) {
  111. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  112. }
  113. } elseif (!is_writable($directory)) {
  114. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  115. }
  116. $target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  117. return new File($target, false);
  118. }
  119. /**
  120. * Returns locale independent base name of the given path.
  121. *
  122. * @param string $name The new file name
  123. *
  124. * @return string containing
  125. */
  126. protected function getName($name)
  127. {
  128. $originalName = str_replace('\\', '/', $name);
  129. $pos = strrpos($originalName, '/');
  130. $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
  131. return $originalName;
  132. }
  133. }