Recursive directory scanner to locate directories and/or files in a file system

2.0.4 2022-05-17 17:12 UTC

This package is auto-updated.

Last update: 2024-09-17 22:17:19 UTC


README

Build Status Code Coverage

Dir is a small library which allows to perform some recursive operations on directories.

Dir::scan()

Gets all nested directories and/or files present inside a directory.

$files = Dir::scan('my/dir',       // Can be a string path of an array of string paths
    [
        'include' => '*.txt',      // Can be an array of includes
        'exclude' => '*.save.txt', // Can be an array of excludes
        'type'    => 'file'        // Can be an array of types, possible values:
                                   // `'file'`, `'dir'`, `'executable'`, `'link'`, `'readable'`, `'writable'`
        'skipDots'       => true,  // Keeps '.' and '..' directories in result
        'leavesOnly'     => true,  // Keeps only leaves
        'followSymlinks' => true,  // Follows Symlinks
        'recursive'      => true   // Scans recursively,
        'copyHandler'    => function($path, $target) { // The copy handler
            copy($path, $target);
        }
    ]
);

Dir::copy()

Copies a directory with files recursively into a destination folder.

$files = Dir::copy('my/dir',       // A string path of an array of string paths
    'my/destination',              // A destination path (string only)
    [
        'mode'           => 0755,  // Mode used for directory creation
        'childrenOnly'   => false, // Copies the file inside 'my/dir' if `true`, otherwise `dir` will be
                                   // added as the root directory.
        'followSymlinks' => true,  // Follows Symlinks
        'recursive'      => true   // Scans recursively
    ]
);

Dir::remove()

Removes a directory and all its content recursively.

Dir::remove('my/dir',     // Can be a string path of an array of string paths
    [
        'followSymlinks' => false,        // Follows Symlinks
        'recursive'      => true,         // Scans recursively
        'include'        => '*.txt',      // Can be an array of includes
        'exclude'        => '*.save.txt', // Can be an array of excludes
    ]
);

Dir::make()

Makes nested directories.

$success = Dir::make('my/dir',  // Can be a string path of an array of string paths
    [
        'mode'      => 0755,         // Mode used for directory creation
        'recursive' => true,         // Scans recursively
        'include'   => '*.txt',      // Can be an array of includes
        'exclude'   => '*.save.txt', // Can be an array of excludes
    ]
);

Dir::tempnam()

Creates a temporary folder (like the tempnam() function but for directories).

$dir = Dir::tempnam(sys_get_temp_dir(), 'mytmp');