Home >Backend Development >PHP Tutorial >PHP code to list all files in a directory_PHP tutorial

PHP code to list all files in a directory_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-21 15:15:401014browse

Copy code The code is as follows:

function dir_path($path) {
$path = str_replace ('\', '/', $path);
if (substr($path, -1) != '/') $path = $path . '/';
return $path;
}
/**
* List all files in the directory
*
* @param str $path directory
* @param str $exts suffix
* @param array $list path array
* @return array Return path array
*/
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;
}
?>

Usage:
Copy Code The 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 in a directory 2

A function written in PHP to list all files in a specified directory.
The function is followed by a sample code of use.
Note: If the page is UTF-8, in the Chinese version of Windows system, garbled characters will appear when reading the Chinese file name.
Copy code The code is as follows:

/* Function listDirTree( $dirName = null )
** Function list all files and subdirectories in the directory
** Parameter $dirName directory name
** Return directory structure array false means 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 directories, recursively
{
$tree[$file] = listDirTree( $filePath );
}
else //For files, add to the current array
{
$tree[] = $file;
}
}
}
closedir( $dh );
}
else
{
exit( "IBFileSystem : can not open directory $dirName.");
}
//Return 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 link address)
echo '
    ';
    for( $i=0; $files [$i] != NULL; $i++ ) {
    echo '
  1. '.$ files[$i].'
  2. ';
    }
    echo '
';
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326054.htmlTechArticleCopy the code as follows: ?php function dir_path($path) { $path = str_replace('\', ' /', $path); if (substr($path, -1) != '/') $path = $path . '/'; return $path; } /** * List directories...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn