Good day to all
When displaying the hierarchy of files in a directory, it incorrectly determines the type of file, what can it be connected with?

<?php function reader_directory($path, $level = 0) { $space = ""; for ($i = 0; $i < $level; $i++) { $space .= "-"; } if (file_exists($path)) { $d = opendir($path); $mass = " "; do { if ($mass != "." && $mass != ".." && $mass != " ") { if (is_dir($mass)) { echo $space . "[DIR]" . $mass . "<br>"; reader_directory($path . "/" . $mass, $level + 5); } elseif (is_file($mass)) { echo $space . "[FILE]" . $mass . "<br>"; } else { echo $space . "[OTHER]" . $mass . "<br>"; } } } while ($mass = readdir($d)); closedir($d); } } reader_directory(".", 1); ?> 

Self output to screen enter image description here
Thank you in advance for the answers.

    2 answers 2

    In is_dir, you must pass the full path, not just the final file name.
    In the upload folder, the text3.txt is of type FILE only because it is the type of the text3.txt file from the root directory.


    Added changes to your code to work correctly.

     <?php function reader_directory($path, $level = 0) { $space = ""; for ($i = 0; $i < $level; $i++) { $space .= "-"; } if (file_exists($path)) { $d = opendir($path); $mass = " "; do { if ($mass != "." && $mass != ".." && $mass != " ") { if (is_dir($path . "/" . $mass)) { echo $space . "[DIR]" . $mass . "<br>" . PHP_EOL; reader_directory($path . "/" . $mass, $level + 5); } elseif (is_file($path . "/" . $mass)) { echo $space . "[FILE]" . $mass . "<br>" . PHP_EOL; } else { echo $space . "[OTHER]" . $mass . "<br>" . PHP_EOL; } } } while ($mass = readdir($d)); closedir($d); } } reader_directory(".", 1); ?> 
    • Can be a little more detail. In the sense of the full path? I have indicated that $ path (this directory) + / + $ mass (another 1 directory) - tweeker
    • @tweeker Completed the answer - ilyaplot
    • Pay attention to is_dir($path . "/" . $mass) is_file($path . "/" . $mass) is_dir($path . "/" . $mass) and is_file($path . "/" . $mass) Mass is_file($path . "/" . $mass) - ilyaplot
    • Krch simply put, it is better to always specify the "root" (main) directory?) - tweeker
    • It is not better to always specify the root, and you must specify the path to the file. $ mass - file name without path. $ path - the path to the file without the file name itself. The program must always know what to do, it can not guess. - ilyaplot

    Use SPL Итераторами :

     function reader_directory($path) { $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); foreach ($iterator as $fileinfo) { echo str_repeat('-', $iterator->getDepth()*5+1); if ($fileinfo->isDir()) echo '[DIR]'; elseif ($fileinfo->isFile()) echo '[FILE]'; else echo '[OTHER]'; echo $fileinfo->getFilename()."\n"; } } reader_directory("."); 
    • Thank you for the answer, but unfortunately, I am still studying php, I haven’t reached it yet. But thank you for writing it out. Later I’ll come and figure it out ) - tweeker