Here you go:
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 end with the indication that not knowing if you use windows or linux used the function pathinfo php that will work in any of the cases.
Updating:
I found after answering that you can improve the function by checking for example if the directory exists and only if it exists and is even a directory is that the function returns a 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.";
}
What do you need?
– Felipe Douradinho
I want a function that takes a path equal to the top path and Return an Array
– Slowaways