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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.