Showing posts with label directory files list.. Show all posts
Showing posts with label directory files list.. Show all posts

Tuesday, April 23, 2013

PHP: Get File Names From A Specified Directory


Get file information from a given directory. Here the following example code to list the .php files in the particular folder.

Code:

$dir = $_SERVER['DOCUMENT_ROOT'].'/OpenInviter/';
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) { 
  if (eregi("\.php",$file)) 
   { # Look at only files with a .php extension
      $string .= "$file";
    $string .= "<br>";
      $fileCount++;
   }
}

if ($fileCount > 0) {
  echo sprintf("<strong>List of Files in <br> %s</strong>
%s<strong>Total Files: %s</strong>",$filePath,$string,$fileCount);
}

Output:

List of Files in  
autoupdate.php
config.php
default.php
example.php
index.php
notifier.php
openinviter.php
stats.php
Total Files: 8

Tuesday, January 29, 2013

Display All files and Contents in a Directory.

Display all files name with content.

(Using RecursiveDirectoryIterator PHP >=5.1.2)

-is_file check the each object values in directory list. suppose file name is occurred display the filename and content.
-highlight_file() display the file content.

<?php

 $path = "C:/xampp/htdocs/samplejoomla/application/controllers/administrator/";

      $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),  RecursiveIteratorIterator::SELF_FIRST);

      foreach($objects as $name => $object){
          echo '<b>'.$name."</b> \n <br/>";
          if(is_file($name))
          {
            echo highlight_file($name);
           
          }
           echo "<br/><br/><br/>";

          }

?>