Directory listing appears with dots, how to remove?

Asked

Viewed 388 times

7

I want to list the image files of a folder, but the first two links that are generated appear like this:

<img class="mySlides" src="Arifureta-shokugyou/capitulo-2/." style="width:100%" />
<img class="mySlides" src="Arifureta-shokugyou/capitulo-2/.." style="width:100%" />

The rest appears the normal link, but the first two always appear with this bug, I do not know how to solve.

Code:

<div class="w3-content" id="box-slider" style="max-width:800px">
    <img class="mySlides" src="public/assets/img/1.jpg" style="width:100%" />
<?php
$path = "Arifureta-shokugyou/capitulo-2/";
$diretorio = dir($path);
$diretorio = $diretorio;

while($arquivo = $diretorio -> read()){
    $img = '<img class="mySlides" src="' . "$path" . "$arquivo" . '" style="width:100%" /> ';
echo $img;

}
$diretorio -> close();
?>
</div>

2 answers

7


It’s not a bug, that’s the way it is.

Every directory has an entry . representing the current level and .. representing the previous:

Screenshot mostrando saída do comando dir, com as entradas em questão

If you don’t want to show these entries, just by a condition first:

if( $arquivo != '.' && $arquivo != '..' )

Applied to your while (and giving a simplified):

while ($arquivo = $diretorio -> read()) {
    if ($arquivo != '.' && $arquivo != '..')
        echo '<img class="mySlides" src="'.$path.$arquivo.'" style="width:100%"> ';
}


As you are dealing with files, has another interesting output:

while ($arquivo = $diretorio -> read()) {
    if (is_file($pasta.$arquivo))
         echo '<img class="mySlides" src="'.$path.$arquivo.'" style="width:100%"> ';
}

The function is_file returns true to directories only. Just need to adapt the variable I used for example ($pasta) for the physical file path.


Right here on the site has another option, if you want to list only certain extensions:

List files from a folder / directory in PHP


Notes:

  • There’s no point in using quotes on variables like you did, this is inappropriate:

    ... . "$path" . "$arquivo" . ...
    //    ^     ^   ^        ^        variáveis não precisam de aspas. Aspas fazem o parser
    //                                de string ser invocado desnecessariamente neste caso.
    
  • If you are using HTML5, there is no tag closure:

    style="width:100%" /> this is XML/XHTML
    style="width:100%">   This is HTML5

1

In PHP we also have the SPL class called FileSystemIterator, that uses an iterator to list the directories of a file.

By default, the FileSystemiterator comes with a second parameter called FileSysteIterator::SKIP_DOTS.

Skeleton of the method:

public __construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ] )

This "Skip Dots" causes the dots to be ignored. It is set by default, but you can also pass it by argument if you wish.

Take an example:

$iterator = new FileSystemIterator($path, FileSystemIterator::SKIP_DOTS);


foreach($iterator as $file) {

    echo '<img class="mySlides" src="' . $path . $file->getFileName(), '" style="width: 100%%">';
}

But details in PHP documentation:

Browser other questions tagged

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