What I am looking for is to make an array that contains only the names of folders (directories) of a given path.
I tried with scandir():
$scan = scandir(realpath(__dir__));
for ($i = 0; $i < count($scan); $i++) {
if (is_file(dirname(__file__) . '\\' . $scan[$i])) {
//Si es un archivo Eliminalo en su posicion $i
array_splice($scan, $i, 1);
}
}
But there is still one .sql file left in the array that shouldn't be there:
[0] => . [1] => .. [3] => controladores [7] => modelos [8] => vistas [9] => zonaedu.sql
//en el último elemento --> ^^^^^^^^^^^
What is this about?
The problem is that you are using one
\
instead of/
, or better yetDIRECTORY_SEPARATOR
. And both is_dir() and is_file() need the full path.List all subdirectories of a folder (non-recursive)
An alternative, if you're also interested in removing symlinks, is to use is_dir() :
Or we can use glob() with the option
GLOB_ONLYDIR
:Or with the DirectoryIterator class :