src/File.php
public static function deleteDirectoryContents(string $directoryPath)
    {
        $paths = scandir($directoryPath);
        if ($paths === false) {
            throw new \Exception("cannot list directory '{$directoryPath}'");
        }

        foreach ($paths as $path) {
            if ($path === '.' || $path === '..') {
                continue;
            }

            $fullPath = "{$directoryPath}/{$path}";

            if (is_dir($fullPath)) {
                self::deleteDirectoryContents($fullPath);//RECURSIVE CALL
                self::throwIfFalse(rmdir($fullPath), "cannot delete '{$fullPath}'", 1);
                continue;
            }

            self::throwIfFalse(unlink($fullPath), "cannot delete '{$fullPath}'", 2);
        }
    }
public static function deleteDirectoryContents(string $directoryPath)
    {
        $paths = scandir($directoryPath);
        if ($paths === false) {
            throw new \Exception("cannot list directory '{$directoryPath}'");
        }

        foreach ($paths as $path) {
            if ($path === '.' || $path === '..') {
                continue;
            }

            $fullPath = "{$directoryPath}/{$path}";

            if (is_dir($fullPath)) {
                self::deleteDirectoryContents($fullPath);//RECURSIVE CALL
                self::throwIfFalse(rmdir($fullPath), "cannot delete '{$fullPath}'", 1);
                continue;
            }

            self::throwIfFalse(unlink($fullPath), "cannot delete '{$fullPath}'", 2);
        }
    }
public static function deleteDirectoryContents ( string $ directoryPath ) { $ paths = scandir ( $ directoryPath ) ; if ( $ paths === false ) { throw new \ Exception ( "cannot list directory '{$directoryPath}'" ) ; } foreach ( $ paths as $ path ) { if ( $ path === '.' || $ path === '..' ) { continue ; } $ fullPath = "{$directoryPath}/{$path}" ; if ( is_dir ( $ fullPath ) ) { self :: deleteDirectoryContents ( $ fullPath ) ; //RECURSIVE CALL self :: throwIfFalse ( rmdir ( $ fullPath ) , "cannot delete '{$fullPath}'" , 1 ) ; continue ; } self :: throwIfFalse ( unlink ( $ fullPath ) , "cannot delete '{$fullPath}'" , 2 ) ; } }
Recursively deletes directory contents

@param string $directoryPath absolute path of directory

@return void

@throws \Exception if file cannot be deleted
@throws \Exception if directory cannot be deleted
@throws \Exception if $directoryPath cannot be listed
Recursively deletes directory contents