Open Journal Systems  3.3.0
lib/vendor/symfony/http-foundation/File/File.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
18 
24 class File extends \SplFileInfo
25 {
34  public function __construct($path, $checkPath = true)
35  {
36  if ($checkPath && !is_file($path)) {
37  throw new FileNotFoundException($path);
38  }
39 
40  parent::__construct($path);
41  }
42 
56  public function guessExtension()
57  {
58  $type = $this->getMimeType();
59  $guesser = ExtensionGuesser::getInstance();
60 
61  return $guesser->guess($type);
62  }
63 
75  public function getMimeType()
76  {
77  $guesser = MimeTypeGuesser::getInstance();
78 
79  return $guesser->guess($this->getPathname());
80  }
81 
92  public function move($directory, $name = null)
93  {
94  $target = $this->getTargetFile($directory, $name);
95 
96  if (!@rename($this->getPathname(), $target)) {
97  $error = error_get_last();
98  throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
99  }
100 
101  @chmod($target, 0666 & ~umask());
102 
103  return $target;
104  }
105 
106  protected function getTargetFile($directory, $name = null)
107  {
108  if (!is_dir($directory)) {
109  if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
110  throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
111  }
112  } elseif (!is_writable($directory)) {
113  throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
114  }
115 
116  $target = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
117 
118  return new self($target, false);
119  }
120 
128  protected function getName($name)
129  {
130  $originalName = str_replace('\\', '/', $name);
131  $pos = strrpos($originalName, '/');
132  $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
133 
134  return $originalName;
135  }
136 }
Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser\getInstance
static getInstance()
Definition: lib/vendor/symfony/http-foundation/File/MimeType/ExtensionGuesser.php:50
Symfony\Component\HttpFoundation\File\File\getMimeType
getMimeType()
Definition: lib/vendor/symfony/http-foundation/File/File.php:75
Symfony\Component\HttpFoundation\File\File\getName
getName($name)
Definition: lib/vendor/symfony/http-foundation/File/File.php:128
Symfony\Component\HttpFoundation\File\File
Definition: lib/vendor/symfony/http-foundation/File/File.php:24
Symfony\Component\HttpFoundation\File\File\guessExtension
guessExtension()
Definition: lib/vendor/symfony/http-foundation/File/File.php:56
Symfony\Component\HttpFoundation\File\File\move
move($directory, $name=null)
Definition: lib/vendor/symfony/http-foundation/File/File.php:92
Symfony\Component\HttpFoundation\File
Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser\getInstance
static getInstance()
Definition: lib/vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php:64
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileNotFoundException.php:19
Symfony\Component\HttpFoundation\File\File\getTargetFile
getTargetFile($directory, $name=null)
Definition: lib/vendor/symfony/http-foundation/File/File.php:106
Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
Definition: lib/vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php:40
Symfony\Component\HttpFoundation\File\Exception\FileException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileException.php:19
Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser
Definition: lib/vendor/symfony/http-foundation/File/MimeType/ExtensionGuesser.php:26
Symfony\Component\HttpFoundation\File\File\__construct
__construct($path, $checkPath=true)
Definition: lib/vendor/symfony/http-foundation/File/File.php:34