How can PHP be used to convert file paths into an array or tree structure?

To convert file paths into an array or tree structure in PHP, you can use the `explode()` function to split the path into individual directories. You can then iterate through each directory, building a nested array or tree structure to represent the file path hierarchy.

function pathToTree($path) {
    $directories = explode('/', $path);
    $tree = [];
    $currentNode = &$tree;

    foreach ($directories as $directory) {
        $currentNode[$directory] = [];
        $currentNode = &$currentNode[$directory];
    }

    return $tree;
}

// Example usage
$path = 'root/dir/subdir/file.txt';
$tree = pathToTree($path);
print_r($tree);