Oihana PHP

hasFiles.php

Table of Contents

Functions

hasFiles()  : bool
Checks if a directory contains at least one file, or only files if strict mode is enabled.

Functions

hasFiles()

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

hasFiles(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 files (no directories 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 file
    if ( hasFiles( $dir ) )
    {
        echo "Directory contains at least one file.\n";
    }
    else
    {
        echo "No files found.\n";
    }

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

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


        
On this page

Search results