<?php
$path_files = getcwd();
// echo $path_files;
$excluded_files = array(
'.htaccess',
'robots.txt',
);
$file_list = get_list_dir($path_files, false, 'file', true, $excluded_files);
preg_match('/(\d+)/', $file_list, $matches);
print_r($matches);
/*
* get list of directory specified
*
* @Author: Birkoff - www.tutorialeonline.net
*
* @param str $path - path where to start reading the directories and files
* @param bool $depth -
* 'true' if you read each subdirectory found in part or
* 'false' if you only read the path specified (not subdirectory)
* @param str $type -
* 'all' - returns all files and directories found,
* 'dir' - only returns the list of directories found,
* 'file' - only returns the list of files found
* @param bool $inc -
* 'true' if desired path and file to that folder / file or
* 'false' returns only if the file name without the path to it
* @param array $exclude - an array of files that can be excluded
*
* @Created by Birkoff for www.tutorialeonline.net
* @return array $list - an array containing numeric values as keys and the
* path / filename as values (depending on the setting parameter $inc)
*/
function get_list_dir($path, $depth = false, $type = 'all', $inc = true, $exclude = array(), $max=95) {
// Set list
$list = array();
// directory element is determined depending on the operating system
$elm = ( stristr(PHP_OS, 'win') === false ) ? '/' : '\\';
if (empty($path))
return false;
if (!is_dir($path))
return false;
// memorizes the current path
$base_path = getcwd();
// change to the path specified
if ($base_path != $path) {
$is_changed = chdir($path);
if (!$is_changed)
return false;
}
$required_path = getcwd();
if (!$required_path)
return false;
// read path required
$director = opendir($required_path);
if (!$director) {
// return to the base path
chdir($base_path);
return false;
}
// reads the current directory
$read = readdir($director);
if ($read === false) {
// return to the base path
chdir($base_path);
return false;
}
while ($read) {
// excluding files / directories unwanted
if (!in_array($read, $exclude)) {
// check what type is required
switch ($type) {
default:
case 'all': // returns all files and directories found
// to memorize what is currently
$list[] = ( $inc ) ? $required_path . $elm . $read : $read;
// if is director and requires completion
if (is_dir($read) && $depth) {
if ( $max<1) {
$list[] = 'Too many subdirectories, indexing interrupted.';
break;
} else {
// browse the directory
$x = get_list_dir($read, $depth, $type, $inc, $exclude, $max-1);
$list = array_merge($list,$x);
}
}
break;
case 'dir': // only returns the list of directories found
// if is director
if (is_dir($read)) {
// to memorize what is currently
$list[] = ( $inc ) ? $required_path . $elm . $read : $read;
// if requires completion
if ($depth) {
if ( $max<1) {
$list[] = 'Too many subdirectories, indexing interrupted.';
break;
} else {
// browse the directory
$x = get_list_dir($read, $depth, $type, $inc, $exclude, $max-1);
$list = array_merge($list,$x);
}
}
}
break;
case 'file': // only returns the list of files found
// check if file
if (is_file($read)) {
// to memorize what is currently
$list[] = ( $inc ) ? $required_path . $elm . $read : $read;
}
// else if is folder and it requires completion
elseif ($depth) {
if ( $max<1) {
$list[] = 'Too many subdirectories, indexing interrupted.';
break;
} else {
// browse the directory
$x = get_list_dir($read, $depth, $type, $inc, $exclude, $max-1);
$list = array_merge($list,$x);
}
}
break;
} // end switch
} // end exclude
// go to next
$read = readdir($director);
} // end while
// director closes
closedir($director);
// returns to the initial path
chdir($base_path);
// return
return $list;
}
?>