23 class File
extends \SplFileInfo
33 public function __construct(
string $path,
bool $checkPath =
true)
35 if ($checkPath && !is_file($path)) {
39 parent::__construct($path);
86 public function move($directory, $name =
null)
90 set_error_handler(
function ($type, $msg) use (&$error) { $error = $msg; });
91 $renamed = rename($this->getPathname(), $target);
92 restore_error_handler();
94 throw new FileException(sprintf(
'Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
97 @chmod($target, 0666 & ~umask());
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));
111 } elseif (!is_writable($directory)) {
112 throw new FileException(sprintf(
'Unable to write in the "%s" directory.', $directory));
115 $target = rtrim($directory,
'/\\').\DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->
getName($name));
117 return new self($target,
false);
129 $originalName = str_replace(
'\\',
'/', $name);
130 $pos = strrpos($originalName,
'/');
131 $originalName =
false === $pos ? $originalName : substr($originalName, $pos + 1);
133 return $originalName;