JBZoo\Utils\FS::rmdir PHP Method

rmdir() public static method

Contributed by Askar (ARACOOL)
public static rmdir ( string $dir, boolean $traverseSymlinks = false ) : boolean
$dir string The directory to be deleted recursively
$traverseSymlinks boolean Delete contents of symlinks recursively
return boolean
    public static function rmdir($dir, $traverseSymlinks = false)
    {
        if (!file_exists($dir)) {
            return true;
        } elseif (!is_dir($dir)) {
            throw new \RuntimeException('Given path is not a directory');
        }
        if (!is_link($dir) || $traverseSymlinks) {
            foreach (scandir($dir) as $file) {
                if ($file === '.' || $file === '..') {
                    continue;
                }
                $currentPath = $dir . '/' . $file;
                if (is_dir($currentPath)) {
                    self::rmdir($currentPath, $traverseSymlinks);
                } elseif (!unlink($currentPath)) {
                    // @codeCoverageIgnoreStart
                    throw new \RuntimeException('Unable to delete ' . $currentPath);
                    // @codeCoverageIgnoreEnd
                }
            }
        }
        // @codeCoverageIgnoreStart
        // Windows treats removing directory symlinks identically to removing directories.
        if (is_link($dir) && !defined('PHP_WINDOWS_VERSION_MAJOR')) {
            if (!unlink($dir)) {
                throw new \RuntimeException('Unable to delete ' . $dir);
            }
        } else {
            if (!rmdir($dir)) {
                throw new \RuntimeException('Unable to delete ' . $dir);
            }
        }
        return true;
        // @codeCoverageIgnoreEnd
    }

Usage Example

コード例 #1
0
ファイル: PathTest.php プロジェクト: JBZoo/Path
 public function setUp()
 {
     $this->_root = FS::clean(__DIR__ . '/test', '/');
     FS::rmdir($this->_root);
     mkdir($this->_root, 0777, true);
     $this->_paths = array($this->_root, $this->_root . DS . 'folder');
 }
All Usage Examples Of JBZoo\Utils\FS::rmdir