Here you have:
function toArray($path) {
$array = [$path];
do {
$path = pathinfo($path)['dirname'];
$array [] = $path;
} while (strlen(basename($path)) > 1);
return $array;
}
var_dump(toArray("C:\Users\Default\AppData\Local"));
return will be:
array(5) { [0]=> string(30) "C:\Users\Default\AppData\Local" [1]=> string(24) "C:\Users\Default\AppData" [2]=> string(16) "C:\Users\Default" [3]=> string(8) "C:\Users" [4]=> string(3) "C:\" }
I will end with the indication that because I do not know if you use windows or linux I used the pathinfo function of php that will work in either case.
Update:
I found after answering that you can improve the function by checking for example if the directory exists and only if it exists and even a directory is that the function returns an array , otherwise returns false .
function toArray($path) {
if (is_dir($path)) {
$array = [$path];
do {
$path = pathinfo($path)['dirname'];
$array [] = $path;
} while (strlen(basename($path)) > 1);
return $array;
}
return false;
}
$arr = toArray("C:\Users\Default\AppData\Local");
if ($arr) {
var_dump($arr);
} else {
echo "ups... o directório não existe ou não se identifica como tal.";
}