PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ke2083/transfans.co.uk-website
PHP | 138 lines | 67 code | 16 blank | 55 comment | 6 complexity | b7a787fead164214fccf2c384501f23a 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\ExtensionGuesser;
  14. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  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. set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
  86. $renamed = rename($this->getPathname(), $target);
  87. restore_error_handler();
  88. if (!$renamed) {
  89. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
  90. }
  91. @chmod($target, 0666 & ~umask());
  92. return $target;
  93. }
  94. protected function getTargetFile($directory, $name = null)
  95. {
  96. if (!is_dir($directory)) {
  97. if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
  98. throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
  99. }
  100. } elseif (!is_writable($directory)) {
  101. throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
  102. }
  103. $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
  104. return new self($target, false);
  105. }
  106. /**
  107. * Returns locale independent base name of the given path.
  108. *
  109. * @param string $name The new file name
  110. *
  111. * @return string containing
  112. */
  113. protected function getName($name)
  114. {
  115. $originalName = str_replace('\\', '/', $name);
  116. $pos = strrpos($originalName, '/');
  117. $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
  118. return $originalName;
  119. }
  120. }