Copy CodeThe code is as follows:
function Dir_path ($path) {
$path = str_replace (' \ \ ', '/', $path);
if (substr ($path,-1)! = '/') $path = $path. '/';
return $path;
}
/**
* List all the files in the directory
*
* @param str $path Directory
* @param str $exts suffix
* @param array $list path
* @return Array return path
*/
function Dir_list ($path, $exts = ", $list = Array ()) {
$path = Dir_path ($path);
$files = Glob ($path. '*');
foreach ($files as $v) {
if (! $exts | | preg_match ("/\. ( $EXTS)/I ", $v)) {
$list [] = $v;
if (Is_dir ($v)) {
$list = Dir_list ($v, $exts, $list);
}
}
}
return $list;
}
?>
How to use:
Copy CodeThe code is as follows:
$r = dir_list (' dir ');
printf ("
The output data is:
%s
\ n ", Var_export ($r, true));
?>
PHP Function-used to list all files under directory 2
A function written in PHP to list all the files in the specified directory.
The function is followed by a sample code that is used.
Note: If the page is Utf-8, in the Windows Chinese version of the system, read the Chinese file name will appear garbled.
Copy CodeThe code is as follows:
/* Function listdirtree ($dirName = null)
* * Feature lists all files and subdirectories under the directory
* * Parameter $dirName directory name
* * return directory structure array false for failure
*/
function Listdirtree ($dirName = null)
{
if (empty ($dirName))
Exit ("Ibfilesystem:directory is empty.");
if (Is_dir ($dirName))
{
if ($dh = Opendir ($dirName))
{
$tree = Array ();
while (($file = Readdir ($DH))!== false)
{
if ($file! = "." && $file! = "...")
{
$filePath = $dirName. "/" . $file;
if (Is_dir ($filePath))//for directory, recursive
{
$tree [$file] = Listdirtree ($filePath);
}
else//is a file that is added to the current array
{
$tree [] = $file;
}
}
}
Closedir ($DH);
}
Else
{
Exit ("Ibfilesystem:can not open Directory $dirName.");
}
Returns the current $tree
return $tree;
}
Else
{
Exit ("Ibfilesystem: $dirName is not a directory.");
}
}
$files = Listdirtree (".");
Print_r ($files);
$size = count (files);
The following code creates a list of files in this directory (with a link address)
Echo '
';
for ($i =0; $files [$i]! = NULL; $i + +) {
Echo '
- '. $files [$i]. '
';
}
Echo '
';
?>
http://www.bkjia.com/PHPjc/326054.html www.bkjia.com true http://www.bkjia.com/PHPjc/326054.html techarticle Copy the code as follows: Php function Dir_path ($path) {$path = str_replace (' \ \ ', '/', $path), if (substr ($path,-1)! = '/') $path = $path. '/'; return $path; }/** * List directory ...