Oihana PHP

hasDirectories.php

Table of Contents

Functions

hasDirectories()  : bool
Checks if a directory contains at least one subdirectory, or only subdirectories if strict mode is enabled.

Functions

hasDirectories()

Checks if a directory contains at least one subdirectory, or only subdirectories if strict mode is enabled.

hasDirectories(string|null $dir[, bool $strict = false ]) : bool
Parameters
$dir : string|null

The path to the directory to check. Must be a valid readable directory.

$strict : bool = false

If true, the function returns true only if the directory contains only subdirectories (no files or other items). Defaults to false.

Tags
throws
DirectoryException

If the path is null, empty, not a directory, or does not meet the readability/writability requirements as checked by assertDirectory().

example
try
{
    $dir = '/path/to/directory';

    // Check if directory contains at least one subdirectory
    if ( hasDirectories($dir) )
    {
        echo "Directory contains at least one subdirectory.\n";
    }
    else
    {
        echo "No subdirectories found.\n";
    }

    // Check if directory contains only subdirectories (strict mode)
    if ( hasDirectories( $dir , true ) )
    {
        echo "Directory contains only subdirectories.\n";
    }
    else
    {
        echo "Directory contains files or other items besides subdirectories.\n";
    }
}
catch (DirectoryException $e)
{
    echo "Error: " . $e->getMessage() . "\n";
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Returns true if the directory contains at least one subdirectory, or if in strict mode, only subdirectories.


        
On this page

Search results