0

I'm making a file explorer with php but I have to put the file in each folder to be able to access it with my index. how can I make it dynamic or recursive and be able to enter with the same file index?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>M07</title>
</head>
<body>

    <?php

        $contingut = "";

        if($handle = opendir('.')){
            while (false !== ($file = readdir($handle))){

                if (($file != ".") && ($file != "..") && ($file != "index.php") && ($file != "desktop.ini")){

                    $contingut .= '<a id="enllaç" href="'.$file.'">'.$file.'</a><br><br>';
                }
            }
            closedir($handle);
        }
?>
<?php echo $contingut ?>
Imad
  • 31
  • 1
  • 6
  • A possible solition can be the `RecursiveDirectoryIterator` class. See here: https://stackoverflow.com/questions/20045622/php-recursivedirectoryiterator – Marcel Sep 29 '22 at 07:54
  • a hint regarding your html: there shouldn't be several elements with the same id. But your echo statement will put out each line with the same id for the element – cypherabe Sep 29 '22 at 08:44

1 Answers1

1
$di = new RecursiveDirectoryIterator('.');

foreach (new RecursiveIteratorIterator($di) as $filename => $file)
{

    if (($file != ".") && ($file != "..") 
    && ($file != "index.php") && ($file != "desktop.ini"))

    {           

        echo '<a id="enllaç" href="'.$file.'">'.$file.'</a><br>';

    }
}
buddemat
  • 4,552
  • 14
  • 29
  • 49
  • yes, but it drops all the directories and files on one page, I want to be able to navigate between directories. @AtherShahzad – Imad Sep 29 '22 at 09:03
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '22 at 22:30