Create directory for $target file if required

@param string $target Path to a file

@return $this
164867
protected function makeDir($target)
    {
        if (!file_exists(dirname($target))) {
            mkdir(dirname($target), 0777, true);
        }

        return $this;
    }
959821
private function copyThenRemove($source, $target)
    {
        if (!is_dir($source)) {
            copy($source, $target);
            $this->unlink($source);

            return;
        }

        $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
        $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
        $this->ensureDirectoryExists($target);

        foreach ($ri as $file) {
            $targetPath = $target.DIRECTORY_SEPARATOR.$ri->getSubPathName();
            if ($file->isDir()) {
                $this->ensureDirectoryExists($targetPath);
            } else {
                copy($file->getPathname(), $targetPath);
            }
        }

        $this->removeDirectoryPhp($source);
    }
309837
public function upload($path, $target)
	{
		$success = false;

		$dir = dirname($target);

		if (!is_dir($dir))
		{
			if (!$this->makeDirectory($dir))
			{
				return $success;
			}
		}

		if (is_writeable($dir) && move_uploaded_file($path, $target))
		{
			if ($this->setPermissions($target))
			{
				$success = true;
			}
		}

		return $success;
	}
154773
public function savePage($target, $html)
    {
        $fs = new Filesystem();
        $fs->dumpFile($this->target . DIRECTORY_SEPARATOR . $target, $html);
    }
505323
protected function normalizeTargetDir($target)
    {
        $target = rtrim(Yii::getAlias($target), '\\/');
        if (file_exists($target)) {
            if (is_dir($target) && !$this->confirm('TargetDirectory already exists. Overwrite?', true)) {
                $this->stderr('User aborted.' . PHP_EOL);

                return false;
            }
            if (is_file($target)) {
                $this->stderr("Error: Target directory \"$target\" is a file!" . PHP_EOL);

                return false;
            }
        } else {
            mkdir($target, 0777, true);
        }

        return $target;
    }
763611
public function symlink($target, $link)
    {
        if (!file_exists($target)) {
            throw new \InvalidArgumentException('Target not found: ' . $target);
        }
        if ($this->relative) {
            $target = $this->makePathRelative($target, dirname($link));
        }
        $this->fs->symlink($target, $link, $this->copyOnWindows);
    }
81382
public static function directoryDiff($source, $target)
    {
        $sourceFiles = self::globr($source, '*');
        $targetFiles = self::globr($target, '*');

        $sourceFiles = array_map(function ($file) use ($source) {
            return str_replace($source, '', $file);
        }, $sourceFiles);

        $targetFiles = array_map(function ($file) use ($target) {
            return str_replace($target, '', $file);
        }, $targetFiles);

        $diff = array_diff($targetFiles, $sourceFiles);

        return array_values($diff);
    }
178181
public static function move($source, $target)
    {
        if (!file_exists($source) || !is_dir($source)) {
            // Rename fails if source folder does not exist.
            throw new \RuntimeException('Cannot move non-existing folder.');
        }

        // Don't do anything if the source is the same as the new target
        if ($source === $target) {
            return;
        }

        if (file_exists($target)) {
            // Rename fails if target folder exists.
            throw new \RuntimeException('Cannot move files to existing folder/file.');
        }

        // Make sure that path to the target exists before moving.
        self::create(dirname($target));

        // Silence warnings (chmod failed etc).
        @rename($source, $target);

        // Rename function can fail while still succeeding, so let's check if the folder exists.
        if (!file_exists($target) || !is_dir($target)) {
            // In some rare cases rename() creates file, not a folder. Get rid of it.
            if (file_exists($target)) {
                @unlink($target);
            }
            // Rename doesn't support moving folders across filesystems. Use copy instead.
            self::copy($source, $target);
            self::delete($source);
        }

        // Make sure that the change will be detected when caching.
        @touch(dirname($source));
        @touch(dirname($target));
        @touch($target);
    }
120204
public function deleteFiles($target)
    {
        if (is_dir($target)) {
            $files = glob($target . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned

            foreach ($files as $file) {
                $this->deleteFiles($file);
            }

            if (is_dir($target)) {
                rmdir($target);
            }

        } elseif (is_file($target)) {
            unlink($target);
        }
    }
640158
public function createRelative($directory, $target, $link)
	{
		if (!is_dir($directory)) {
			$this->error("Directory '$directory' not found.");
		}
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
			$cmd = 'cd ' . escapeshellarg($directory) . ' && mklink ' . ' /D ' . escapeshellarg($link) . ' ' . escapeshellarg($target);
		} else {
			$cmd = 'cd ' . escapeshellarg($directory) . ' && ln -s ' . escapeshellarg($target) . ' ' . escapeshellarg($link);
		}
		$command = new Commands\Exec();
		$command->setCommand($cmd);
		$result = $command->execute();
		if ($result->getResult() !== 0) {
			$this->error("Cannot create symlink '$target' - '$link' in directory '$directory'.");
		}
		return $result;
	}
714984
public function directory($path)
    {
        if (!File::exists($path)) {
            File::makeDirectory($path, 0775, true, true);
        }
        return $this;
    }
787774
static function copyFile($source, $target, $overwrite = true)
    {
        if (!file_exists($source)) return false;
        if (file_exists($target) && $overwrite == false) return false;
        elseif (file_exists($target) && $overwrite == true) {
            if (!unlink($target)) return false;
        }
        $targetDir = dirname($target);
        if (!self::createDirectory($targetDir)) return false;
        return copy($source, $target);
    }
167642
public static function copy($source, $target)
    {
        $source = Normalize::diskFullPath($source);
        $target = Normalize::diskFullPath($target);

        // check if target directory exist & create new if not
        $targetArray = explode(DIRECTORY_SEPARATOR, $target);
        array_pop($targetArray);
        $targetDir = implode(DIRECTORY_SEPARATOR, $targetArray);
        if (!Directory::exist($targetDir)) {
            Directory::create($targetDir, 0777);
        }

        return copy($source, $target);
    }
424292
private function mkdir($target)
    {
        if (\is_dir($target)) {
            return;
        }

        if (! $this->file->mkdir($target)) {
            $this->output->writeln("<error>[$target] could not be created. Please check directory permissions.</error>");
        }
    }
24863
private static function create(string $target): OutputStream
    {
        $out      = new self(fopen($target, 'w'));
        $encoding = self::detectOutputEncoding();
        if ('UTF-8' !== $encoding) {
            $out = new EncodingOutputStream($out, $encoding . '//IGNORE');
        }

        return $out;
    }
1021241
public function compile($configFile, $force, $target)
    {
        $this->execute($this->commandGenerator->compile($configFile, $force, $target));

        return $this;
    }
154563
public function copy($source, $target)
    {
        if (!is_dir($source)) {
            return copy($source, $target);
        }
        $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
        /** @var RecursiveDirectoryIterator $ri */
        $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
        $this->filesystem->ensureDirectoryExists($target);
        $result = true;
        foreach ($ri as $file) {
            $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
            if ($file->isDir()) {
                $this->filesystem->ensureDirectoryExists($targetPath);
            } else {
                $result = $result && copy($file->getPathname(), $targetPath);
            }
        }
        return $result;
    }