Showing posts with label read files from directory in php. Show all posts
Showing posts with label read files from directory in php. 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