Oihana PHP

treePaths.php

Table of Contents

Functions

treePaths()  : array<int, string>
Lists every path under a directory — files, directories and links — relative to a root.

Functions

treePaths()

Lists every path under a directory — files, directories and links — relative to a root.

treePaths(string $directory[, string $root = '' ]) : array<int, string>

Unlike recursiveFilePaths(), which returns the files only, this lists the directories too, so two trees can be compared entry by entry: what one holds and the other does not.

Symbolic links are listed and never followed. A link pointing at / would otherwise list the whole machine, and a caller removing "what the other tree does not have" would be handed paths outside the directory it was asked about. For the same reason, a $directory that is itself a link gives an empty list.

A sub-directory that cannot be read is listed, and its content is skipped; a $directory that cannot be read gives an empty list. Paths always use /, whatever the system.

Parameters
$directory : string

The directory to walk.

$root : string = ''

What the paths are made relative to — the directory itself when empty. Give a parent to keep the directory's own name in each path.

Tags
example
use function oihana\files\treePaths;

// /srv/site/content/a.txt and /srv/site/content/img/b.png

treePaths( '/srv/site/content' ) ;
// [ 'a.txt' , 'img' , 'img/b.png' ]

treePaths( '/srv/site/content' , '/srv/site' ) ;
// [ 'content/a.txt' , 'content/img' , 'content/img/b.png' ]
author

Marc Alcaraz (ekameleon)

since
1.7.0
Return values
array<int, string> —

The paths, in the order the filesystem returns them, each directory before its content. Empty when the directory is not there.

On this page

Search results