Open Journal Systems  3.3.0
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 
17 
23 class File extends \SplFileInfo
24 {
33  public function __construct(string $path, bool $checkPath = true)
34  {
35  if ($checkPath && !is_file($path)) {
36  throw new FileNotFoundException($path);
37  }
38 
39  parent::__construct($path);
40  }
41 
55  public function guessExtension()
56  {
57  return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null;
58  }
59 
71  public function getMimeType()
72  {
73  return MimeTypes::getDefault()->guessMimeType($this->getPathname());
74  }
75 
86  public function move($directory, $name = null)
87  {
88  $target = $this->getTargetFile($directory, $name);
89 
90  set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
91  $renamed = rename($this->getPathname(), $target);
92  restore_error_handler();
93  if (!$renamed) {
94  throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
95  }
96 
97  @chmod($target, 0666 & ~umask());
98 
99  return $target;
100  }
101 
105  protected function getTargetFile($directory, $name = null)
106  {
107  if (!is_dir($directory)) {
108  if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
109  throw new FileException(sprintf('Unable to create the "%s" directory.', $directory));
110  }
111  } elseif (!is_writable($directory)) {
112  throw new FileException(sprintf('Unable to write in the "%s" directory.', $directory));
113  }
114 
115  $target = rtrim($directory, '/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));
116 
117  return new self($target, false);
118  }
119 
127  protected function getName($name)
128  {
129  $originalName = str_replace('\\', '/', $name);
130  $pos = strrpos($originalName, '/');
131  $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
132 
133  return $originalName;
134  }
135 }
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\guessExtension
guessExtension()
Definition: vendor/symfony/http-foundation/File/File.php:55
Symfony\Component\HttpFoundation\File\File\move
move($directory, $name=null)
Definition: vendor/symfony/http-foundation/File/File.php:86
Symfony\Component\HttpFoundation\File
Symfony\Component\Mime\MimeTypes\getDefault
static getDefault()
Definition: MimeTypes.php:69
Symfony\Component\HttpFoundation\File\File\__construct
__construct(string $path, bool $checkPath=true)
Definition: vendor/symfony/http-foundation/File/File.php:33
Symfony\Component\Mime\MimeTypes
Definition: MimeTypes.php:37
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\Exception\FileException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileException.php:19