is_dir() according to the php manual returns true if passed a string that refers to a folder.
It’s returning false — despite being passed an array of valid folder names — this is probably something simple but I’m beating myself up over not finding it.
Here is the code:
define('TEST_SUBFOLDERS_FILES_PATH', 'C:/xampp/htdocs/theProj/mainSubdir/');
$currentFolder = ' ';
$rootOfmainSubdir = TEST_SUBFOLDERS_FILES_PATH';
$theArrayOfDirsAndFilesInmainSubdir;
// fill an array that contains all files and folders under the /mainSubdir folder
// using php's 'scandir()'
if( ($theArrayOfDirsAndFilesInmainSubdir = scandir($rootOfmainSubdir )) != FALSE )
{
$numArrayEntries = count($theArrayOfDirsAndFilesInmainSubdir);
echo "<br><br>The number of folders/files found is: " . $numArrayEntries;
// now do the triage on subfolders under /mainSubdir versus files
for($i = 0; $i < $numArrayEntries; $i++)
{
echo "<br><br>Current array element is: "
. $theArrayOfDirsAndFilesInmainSubdir[$i]
. " and is_dir() on this element returns --> "
. var_dump( is_dir($theArrayOfDirsAndFilesInmainSubdir[$i]) );
// rest of code
Here is what I see on the output for almost all the folders:
Current array element is: testFolderN and is_dir() on this element returns -->
boolean false
OR I see:
Current array element is: . and is_dir() on this element returns -->
boolean true
AND:
Current array element is: .. and is_dir() on this element returns -->
boolean true
I’m missing something really dumb here but sheesh. It seems that is_dir() is correctly returning TRUE only for the current folder “.” and the parent folder “..”
If there is a nuance I’m missing in the use of is_dir() I’m not seeing it.
Help is appreciated — all I’m going to do in the code is to make a list of the current subfolders under the TEST_SUBFOLDERS_FILES_PATH if I can get is_dir() to return TRUE not only for “.” and “..” but also the other ‘testFolderN’ subfolders.
By the way — there is a total of 5 subfolders, and one file in the directory, so the display of the count above is:
The number of folders/files found is: 8
You scan the
$rootOfmainSubdir, but you test foris_dir()ness in your current directory. You can either prepend the root and directory separator before checking, orchdir()in there. Note, that your current directory also has.and..entries.