List directories and PHP files

Asked

Viewed 875 times

0

Hello, I have a PHP code that works well for what was proposed, but I need some adjustments to it.
The code in question already lists the directories and the files, but in directories that I have files and subdirectories it lists me shuffled.
I would like the code to separate what is a file and what is a directory, showing the directories at the beginning of the list, and the acquisitions afterwards.
If possible after separation, also list alphabetically within your type.
Example of expected output:

Bee/
Rumours/
Koala/
abelhass.
png.
curd.png

My knowledge in PHP is practically null, but it was the best alternative I could find. If anyone can help me adapt the code, I’d really appreciate it;

<html lang="pt-br">
<head>
    <title class="title h1"></title>
    <meta http-equiv="Content-Language" content="pt-br" charset="ISO-8859-1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Fondamento|Hanuman|Kaushan+Script|Nanum+Pen+Script|Orbitron&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://fonts.googleapis.com/css?family=Comfortaa&display=swap" rel="stylesheet">

</head>
<style type="text/css">
    .bg-grey{
        background-color: #E6E6E6;
    }
    .bg-azulClarrisimo{
        background-color: #E0F2F7;
    }
    .title{
        font-family: 'Fondamento', cursive;
    }
    .comfortaa{
        font-family: 'Comfortaa', cursive;
    }
    @media only screen and (max-width: 900px) {

      .tamanho{
        font-size: 10px;
      }
    }

</style>
<body>
    <?php
      //diretorio
        $baseDir = './';
        $abreDir = (array_key_exists('dir', $_GET) ? $_GET['dir'] : $baseDir);
        $openDir = dir($abreDir);

        $strrdir = strrpos(substr($abreDir,0,-1),'/');

        $backDir = substr($abreDir,0,$strrdir+1);                                       


        if($abreDir != $baseDir){
            echo '<a class="btn btn-warning " href="index.php?dir='.$backDir.'"><i class="fa fa-arrow-left"></i>Voltar</a>';
        }
        echo '<hr>';
        echo '<table class="text-left table table-dark reponsive tamanho" width="100%" cellspacing="0" cellspadding="">';                 
        while($arq = $openDir -> read()): 
            if($arq != '.' && $arq != '..' &&  $arq != 'index.php'):                
                if(is_dir($abreDir.$arq)){
                //pastas                                                        
                    echo '<tr>';    
                    echo '<td class="alert alert-info text-left tamanho"><a class="form-control btn text-left tamanho" href="index.php?dir='.$abreDir.$arq.'/"><i class="fa fa-bars"></i>&nbsp&nbsp&nbsp'.$arq.'</a></td>';
                    echo '</tr>';    
                }else{
                //arquivos
                    echo '<tr>';                                                            
                    echo '<td class="alert text-right tamanho"><a class="form-control btn text-white text-right tamanho" href="'.$abreDir.$arq.'" target="_blank">'.$arq.' &nbsp&nbsp&nbsp <i class="fa fa-download" ></i></a></td>';  
                    echo '</tr>';                                
                }                    
            endif;              
        endwhile;
        echo '</table>';
        echo '<hr>';
        if($abreDir != $baseDir){
            echo '<a class="btn btn-warning" href="index.php?dir='. $backDir.'"><i class="fa fa-arrow-left"></i>Voltar</a>';
        }                                       

    ?>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

1 answer

2

I think that solves your problem:

<?php
    $itens = new DirectoryIterator('./');
    foreach($itens as $item){
        if($item->gettype() === 'dir'){
            $diretorios[] = $item->getFilename();
        }else{
            $arquivos[] = $item->getFilename();
        }

    }


    foreach($diretorios as $diretorio){
        echo '<tr>';    
        echo '<td class="alert alert-info text-left tamanho">'.$diretorio.'</td>';
        echo '</tr><br>';
    }

    foreach($arquivos as $arquivo){
        echo '<tr>';
        echo '<td class="alert text-right tamanho">'.$arquivo.'</td>';
        echo '</tr><br>';
    }?>

Directoryiterator will scan the directory and the first foreach will iterate over the items if will array the directories of the files and the second foreach will iterate over the arrey of the directories and the third will do the same about the files.

A way to link subdirectories without using multiple files. php would be storing in a variable the directory you want to list the content and pass via get this parameter.

<?php
$dir = isset($_GET['dir']) ? './'.$_GET['dir']: './';

$itens = new DirectoryIterator("$dir");
   foreach($itens as $item){
    if($item->gettype() === 'dir'){
        $diretorios[] = $item->getFilename();
    }else{
        $arquivos[] = $item->getFilename();
    }

}


foreach($diretorios as $diretorio){
    echo '<tr>';    
    echo '<td class="alert alert-info text-left tamanho"><a href="?dir='.$diretorio.'">/'.$diretorio.'</a></td>';
    echo '</tr><br>';
}

foreach($arquivos as $arquivo){
    echo '<tr>';
    echo '<td class="alert text-right tamanho"><a href="/'.$arquivo.'">'.$arquivo.'</a></td>';
    echo '</tr><br>';
}?>

if you want to block direct access to directories you can use. htaccess and improve using friendly urls, take a look at google and here in the stack too, there is a lot about that

  • I know it’s a lot to ask, but could you help me adapt your code to mine? My knowledge of PHP is null. Your code works well, but only works in the initial directory. One solution I found using your code was to put an index.php file in each directory and sub-directory. But it is very amateur and nothing practical and functional.

  • takes a look that I edited the answer including something that should help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.