Directory listing appears with dots, how to remove?

7

I want to list image files in 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, which 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>
    
asked by anonymous 24.10.2018 / 02:18

2 answers

7

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

Every directory has an entry . that represents the current level and .. that represents the previous one:

Ifyoudonotwanttoshowtheseentries,justoneconditionbefore:

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

Appliedtoyourwhile(andgivingasimplifiedone):

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


As you are dealing with files, you have 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 for directories only. Just need to adapt the example variable ( $pasta ) to the physical path of the file.


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

  

List files from a folder / directory in PHP


Notes:

  • There's no sense in using quotation marks on variants 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 closing:

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

24.10.2018 / 02:47
1

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

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

Method Skeleton:

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

This skip dots causes points to be ignored. It is set by default, but you can also pass it by argument if you wish.

See 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:

24.10.2018 / 15:44