List folder files with PHP

Asked

Viewed 2,344 times

1

I was looking for this solution, where I can browse the server and show the folders and files, without using BD.

this sounds interesting, but how can I show all the files without showing the first 2 (.) (..) inserir a descrição da imagem aqui

This is what happens to me, what I want is to show me the 5 videos and hide the first 2.

inserir a descrição da imagem aqui

it is possible to do?

I’m using the following code

<?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();

?>

3 answers

2

Use Filesystemiterator.

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

Take an example:

$iterator = new FileSystemIterator($dir);

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

Note: This class is available as of PHP 5.3.

See more in:

1

You can do the name check like this $arquivo === '.' || $arquivo === '..', if it’s any of these use the continue; to jump to the next file.

That should do it:

<?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 the glob to filter only for specific extensions, thus:

  • Imagery:

    glob('/pasta/foobar/baz/*.{png,gif,jpg,jpeg}', GLOB_BRACE)
    
  • Videos (I believe this is 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>
        ";
}

?>
  • worked perfectly in windows environment, tested on linux and appeared 1 register '.DS_S'' other '._. DS_S'

  • @Salagdo Mac OSX generates these files, in Finder they are invisible, but PHP can see them, I will edit the code and I warn you.

  • @Salagdo ready, edited response, modified the first code and added an example with glob which facilitates much more :D

0

Using the SPL, you can do it this way:

foreach (new DirectoryIterator('../../../../SAV-Videos/Racing-Division/') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\n";
}
  • Could be, @Guilhermenascimento. It’s an alternative and he decides the best option, since you’ve already commented on FileSystemIterator.

Browser other questions tagged

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