<?php if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." ) { echo "$entry\n"."<br>"; } } closedir($handle); } ?> 

How to make so that only .html files are viewed?

  • use the function glob("*.html") - teran
  • or add conditions to verify that the file name ends in html to what you already have with . and .. - teran 2:41
  • @teran and how? Google is not 1 hour, all the bullshit through the glob which I do not know how to fasten. - Pavel

2 answers 2

For these purposes, you can use the glob() function, which provides a search for files by a given mask.

 <?php foreach (glob("*.html") as $filename) { echo "$filename\n<br>"; } 

If you also need to specify the directory to search for (in addition to the current one), then also specify it in the search mask, for example, glob("/var/log/*.log") .

  • question how to twist it into my code? I have seen all the solutions, but I don’t know how to cross it all - Pavel
  • @Pavel replaced entirely - teran
  • earned thanks, do not tell me how the file renaming is organized, so I brought them up - Pavel
  • one
    @Pavel try reading help on pkhp. Everything is written there. For renaming, there is a function rename() , whose parameters are the current file name, and a new one. - teran

Take a look at the SPL, there you can find a lot of ready-made solutions.

 <?php foreach (new DirectoryIterator('.') as $fileInfo) { if (!$fileInfo->isDot()) continue; if ($fileInfo->getExtension() == 'html') { echo $fileInfo->getFilename() . "\n"; } } 
  • one
    not better isDir() check in the first condition? - teran 2:51
  • The question is how to twist it into my code? I saw all the solutions but I don `t know how to cross everything. I have already found this solution, it is this, at the end the code is still not closed - Pavel
  • did not understand you. what does “cross” mean? :) - myxaxa