List Folder Files with PHP

2

I was looking for this solution, where I can browse the server and show folders and files, without resorting to DB.

This seems interesting to me, but how can I show all files without the first 2 (.) (..)

Thisiswhathappenstome,whatIwantistoshowmethe5videosandhidethefirst2.

Isitpossibletodoit?

I'musingthefollowingcode

<?php$path="../../../../SAV-Videos/Racing-Division/";
    $diretorio = dir($path);
    //echo "Lista de Arquivos do diretório '<strong>".$path."</strong>':<br />";    
    while($arquivo = $diretorio -> read()){
        $nom_directorio = substr($arquivo,0,-4);
        $extension = substr($arquivo,-3);

        echo "<div class='col-md-3 col-sm-6 col-xs-12'>
              <div class='info-box'>
                <div align='center'>
                <div class='demonstrations'>
                <video width='100%' height='200' controls='controls' style='margin-top: -20px;'>
                <source src='../../../../SAV-Videos/Racing-Division/$arquivo' type='video/mp4'>
                <object data='' width='100%' height='200'>
                <embed width='100% height='200' src='../../../../SAV-Videos/Racing-Division/$arquivo'>
                </object>
                </video>
              </div>
              <br/>
              <div class='info-box-text' style='padding: 5px;'><b>".$nom_directorio."</b></div>
              <span class='info-box-text'>
                <b>EXTENSION:</b> ".$extension."
              </span>
              <div style='padding: 10px;'>
              <a href='../../../../SAV-Videos/Racing-Division/$arquivo' class='btn btn-default' rel='group' download style='margin-top: 5px;'
              title='Télécharger ".$nom_directorio."'>
              <i class='fa fa-download'></i>&nbsp;
              </a>
              &nbsp;
              <a href='Suprimer_video.php?nome=$arquivo&division=$categorie' class='btn btn-default onClick='return confirma();'>
              <i class='fa fa-trash'></i>
              </a>
              </div>
              </div>
              <!-- /.info-box-content -->
              </div>
              <!-- /.info-box -->
            </div>
            ";
    }
    $diretorio -> close();

?>
    
asked by anonymous 21.06.2017 / 17:58

3 answers

1

Use FileSystemIterator .

The FileSystemIterator class is an iterator whose purpose is to iterate over a directory structure. Unlike DirectoryIterator and Diretory , this class does not consider . or .. at the moment of listing files.

See an example:

$iterator = new FileSystemIterator($dir);

foreach ($iterator as $file) {
     echo $file->getFilename(), PHP_EOL;
}

Note : This class is available from version 5.3 of PHP.

See more at:

21.06.2017 / 18:15
1

You can do the check by name so $arquivo === '.' || $arquivo === '..' , if any of these uses continue; to jump to the next file.

So you should resolve:

<?php
    $path = "./";
    $diretorio = dir($path);
    //echo "Lista de Arquivos do diretório '<strong>".$path."</strong>':<br />";    
   $ignorados = array(
       '.ds_store', //Arquivo de configuração de pasta do Mac
       '._.ds_s', //Arquivo de configuração/cache do Mac
       'desktop.ini', //Arquivo de configuração de pasta do Win
       'thumbs.db'  //Arquivo de cache de imagens do Win
   );

    while($arquivo = $diretorio -> read()){
        $narq = strtolower($arquivo);

        if (in_array($narq, )) {
            continue;
        }

        $nom_directorio = substr($arquivo,0,-4);
        $extension = substr($arquivo,-3);

        echo "<div class='col-md-3 col-sm-6 col-xs-12'>

You can also use glob to filter only for specific extensions, like this:

  • Images:

    glob('/pasta/foobar/baz/*.{png,gif,jpg,jpeg}', GLOB_BRACE)
    
  • Videos (I believe it's your case):

    glob('/pasta/foobar/baz/*.{mp4,webm,mov,mpeg,mpg}', GLOB_BRACE)
    

The code should look like this:

<?php
$path = "../../../../SAV-Videos/Racing-Division/";

foreach (glob($path . '*.{mp4,webm,mov,mpeg,mpg}', GLOB_BRACE) as $arquivo) {
    $nom_directorio = substr($arquivo,0,-4);
    $extension = substr($arquivo,-3);


    echo "<div class='col-md-3 col-sm-6 col-xs-12'>
          <div class='info-box'>
            <div align='center'>
            <div class='demonstrations'>
            <video width='100%' height='200' controls='controls' style='margin-top: -20px;'>
            <source src='../../../../SAV-Videos/Racing-Division/$arquivo' type='video/mp4'>
            <object data='' width='100%' height='200'>
            <embed width='100% height='200' src='../../../../SAV-Videos/Racing-Division/$arquivo'>
            </object>
            </video>
          </div>
          <br/>
          <div class='info-box-text' style='padding: 5px;'><b>".$nom_directorio."</b></div>
          <span class='info-box-text'>
            <b>EXTENSION:</b> ".$extension."
          </span>
          <div style='padding: 10px;'>
          <a href='../../../../SAV-Videos/Racing-Division/$arquivo' class='btn btn-default' rel='group' download style='margin-top: 5px;'
          title='Télécharger ".$nom_directorio."'>
          <i class='fa fa-download'></i>&nbsp;
          </a>
          &nbsp;
          <a href='Suprimer_video.php?nome=$arquivo&division=$categorie' class='btn btn-default onClick='return confirma();'>
          <i class='fa fa-trash'></i>
          </a>
          </div>
          </div>
          <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        ";
}

?>
    
21.06.2017 / 18:14
0

Using SPL , you can do as follows:

foreach (new DirectoryIterator('../../../../SAV-Videos/Racing-Division/') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}
    
21.06.2017 / 18:19