Jump to content

List only some extention (File Listing)


Peuplarchie

Recommended Posts

Good day to you all,

            I'm working on a pice of code which list all files in a directory and return the list and its content right under the name of the file.

 


<?php
$thelist = "";
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
          	$thelist .= '<a href="'.$file.'">'.$file.'</a><br/>';
            $contents = file($file);
            $string = implode($contents);
            $thelist .= '<p>'.$string.'</p>';
            
          }
       }
  closedir($handle);
  }
?>
<P>List of files:</p>
<P><?=$thelist?></p>

 

 

I'm looking for a way of choose only 3 or 4 extension only.

 

How would I dow so ?

 

Thanks !

 

Link to comment
https://forums.phpfreaks.com/topic/132044-list-only-some-extention-file-listing/
Share on other sites

Since PHP is loaded with functions, here's a way to use a couple of them to shorten your code:

<pre>
<?php
$thelist = "";
foreach (glob("*.{htm,php,txt}",GLOB_BRACE) as $file) {
    $thelist .= "<a href='$file'>$file</a><br/><p>".htmlentities(file_get_contents($file))."</p>";
}
?>
<P>List of files:</p>
<P><?=$thelist?></p>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.