如何在没有文件.
(当前目录)或..
(父目录)的情况下获取给定目录的所有子目录
然后在函数中使用每个目录?
答案 0 :(得分:187)
答案 1 :(得分:138)
以下是如何仅使用GLOB检索目录:
$directories = glob($somePath . '/*' , GLOB_ONLYDIR);
答案 2 :(得分:36)
Spl DirectoryIterator类提供了一个用于查看文件系统目录内容的简单界面。
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo $fileinfo->getFilename().'<br>';
}
}
答案 3 :(得分:25)
与previous question中的几乎相同:
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($yourStartingPath),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
echo strtoupper($file->getRealpath()), PHP_EOL;
}
}
将strtoupper
替换为您想要的功能。
答案 4 :(得分:5)
试试这段代码:
<?php
$path = '/var/www/html/project/somefolder';
$dirs = array();
// directory handle
$dir = dir($path);
while (false !== ($entry = $dir->read())) {
if ($entry != '.' && $entry != '..') {
if (is_dir($path . '/' .$entry)) {
$dirs[] = $entry;
}
}
}
echo "<pre>"; print_r($dirs); exit;
答案 5 :(得分:4)
在数组中:
function expandDirectoriesMatrix($base_dir, $level = 0) {
$directories = array();
foreach(scandir($base_dir) as $file) {
if($file == '.' || $file == '..') continue;
$dir = $base_dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($dir)) {
$directories[]= array(
'level' => $level
'name' => $file,
'path' => $dir,
'children' => expandDirectoriesMatrix($dir, $level +1)
);
}
}
return $directories;
}
//访问:
$dir = '/var/www/';
$directories = expandDirectoriesMatrix($dir);
echo $directories[0]['level'] // 0
echo $directories[0]['name'] // pathA
echo $directories[0]['path'] // /var/www/pathA
echo $directories[0]['children'][0]['name'] // subPathA1
echo $directories[0]['children'][0]['level'] // 1
echo $directories[0]['children'][1]['name'] // subPathA2
echo $directories[0]['children'][1]['level'] // 1
显示所有内容的示例:
function showDirectories($list, $parent = array())
{
foreach ($list as $directory){
$parent_name = count($parent) ? " parent: ({$parent['name']}" : '';
$prefix = str_repeat('-', $directory['level']);
echo "$prefix {$directory['name']} $parent_name <br/>"; // <-----------
if(count($directory['children'])){
// list the children directories
showDirectories($directory['children'], $directory);
}
}
}
showDirectories($directories);
// pathA
// - subPathA1 (parent: pathA)
// -- subsubPathA11 (parent: subPathA1)
// - subPathA2
// pathB
// pathC
答案 6 :(得分:2)
<?php
/*this will do what you asked for, it only returns the subdirectory names in a given
path, and you can make hyperlinks and use them:
*/
$yourStartingPath = "photos\\";
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($yourStartingPath),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
$path = strtoupper($file->getRealpath()) ;
$path2 = PHP_EOL;
$path3 = $path.$path2;
$result = end(explode('/', $path3));
echo "<br />". basename($result );
}
}
/* best regards,
Sanaan Barzinji
Erbil
*/
?>
答案 7 :(得分:1)
您可以尝试此功能(需要PHP 7)
function getDirectories(string $path) : array
{
$directories = [];
$items = scandir($path);
foreach ($items as $item) {
if($item == '..' || $item == '.')
continue;
if(is_dir($path.'/'.$item))
$directories[] = $item;
}
return $directories;
}
答案 8 :(得分:1)
正确的方式
<sourceFileExcludes>
<exclude></exclude>
<exclude></exclude>
</sourceFileExcludes>
灵感来自Laravel
答案 9 :(得分:0)
对于实际上想要没有文件的文件夹和子文件夹的人,就像OP说的那样,以下代码既输出文件夹列表又输出子文件夹,以及相同的数组。
<?php
/**
* Function for recursive directory file list search as an array.
*
* @param mixed $dir Main Directory Path.
*
* @return array
*/
function listFolderFiles($dir)
{
$fileInfo = scandir($dir);
$allFileLists = [];
foreach ($fileInfo as $folder) {
if ($folder !== '.' && $folder !== '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $folder) === true) {
$allFileLists[$folder . '/'] = listFolderFiles($dir . DIRECTORY_SEPARATOR . $folder);
echo ' '. $folder. ' ' <br>';
} else {
echo' ';
}
}
}
return $allFileLists;
}//end listFolderFiles()
listFolderFiles('C:\wamp64\www\code');
$dir = listFolderFiles('C:\wamp64\www\code');
echo '<pre>';
print_r($dir);
echo '</pre>'
?>
答案 10 :(得分:0)
这是一个班轮代码:
$sub_directories = array_map('basename', glob($directory_path . '/*', GLOB_ONLYDIR));
答案 11 :(得分:0)
direct asked this被错误地关闭的唯一问题,所以我必须把它放在这里。
它还具有过滤目录的功能。
/**
* Copyright © 2020 Theodore R. Smith <https://www.phpexperts.pro/>
* License: MIT
*
* @see https://stackoverflow.com/a/61168906/430062
*
* @param string $path
* @param bool $recursive Default: false
* @param array $filtered Default: [., ..]
* @return array
*/
function getDirs($path, $recursive = false, array $filtered = [])
{
if (!is_dir($path)) {
throw new RuntimeException("$path does not exist.");
}
$filtered += ['.', '..'];
$dirs = [];
$d = dir($path);
while (($entry = $d->read()) !== false) {
if (is_dir("$path/$entry") && !in_array($entry, $filtered)) {
$dirs[] = $entry;
if ($recursive) {
$newDirs = getDirs("$path/$entry");
foreach ($newDirs as $newDir) {
$dirs[] = "$entry/$newDir";
}
}
}
}
return $dirs;
}
答案 12 :(得分:-1)
递归查找所有PHP文件。逻辑应该足够简单,可以通过避免函数调用来实现快速(呃)。
import index.py
答案 13 :(得分:-1)
您可以使用glob()函数执行此操作。
答案 14 :(得分:-1)
如果您正在寻找列出解决方案的递归目录。使用下面的代码我希望它可以帮助你。
<?php
/**
* Function for recursive directory file list search as an array.
*
* @param mixed $dir Main Directory Path.
*
* @return array
*/
function listFolderFiles($dir)
{
$fileInfo = scandir($dir);
$allFileLists = [];
foreach ($fileInfo as $folder) {
if ($folder !== '.' && $folder !== '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $folder) === true) {
$allFileLists[$folder . '/'] = listFolderFiles($dir . DIRECTORY_SEPARATOR . $folder);
} else {
$allFileLists[$folder] = $folder;
}
}
}
return $allFileLists;
}//end listFolderFiles()
$dir = listFolderFiles('your searching directory path ex:-F:\xampp\htdocs\abc');
echo '<pre>';
print_r($dir);
echo '</pre>'
?>
答案 15 :(得分:-1)
我编写了一个运行良好的扫描程序,并扫描每个目录和文件中的目录和子目录。
function scanner($path){
$result = [];
$scan = glob($path . '/*');
foreach($scan as $item){
if(is_dir($item))
$result[basename($item)] = scanner($item);
else
$result[] = basename($item);
}
return $result;
}
示例强>
var_dump(scanner($path));
返回:
array(6) {
["about"]=>
array(2) {
["factory"]=>
array(0) {
}
["persons"]=>
array(0) {
}
}
["contact"]=>
array(0) {
}
["home"]=>
array(1) {
[0]=>
string(5) "index.php"
}
["projects"]=>
array(0) {
}
["researches"]=>
array(0) {
}
[0]=>
string(5) "index.php"
}
答案 16 :(得分:-1)
以下递归函数返回一个包含子目录完整列表的数组
function getSubDirectories($dir)
{
$subDir = array();
$directories = array_filter(glob($dir), 'is_dir');
$subDir = array_merge($subDir, $directories);
foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory.'/*'));
return $subDir;
}
来源:https://www.lucidar.me/en/web-dev/how-to-get-subdirectories-in-php/
答案 17 :(得分:-2)
查找指定目录下的所有文件和文件夹。
function scanDirAndSubdir($dir, &$fullDir = array()){
$currentDir = scandir($dir);
foreach ($currentDir as $key => $val) {
$realpath = realpath($dir . DIRECTORY_SEPARATOR . $val);
if (!is_dir($realpath) && $filename != "." && $filename != "..") {
scanDirAndSubdir($realpath, $fullDir);
$fullDir[] = $realpath;
}
}
return $fullDir;
}
var_dump(scanDirAndSubdir('C:/web2.0/'));
array (size=4)
0 => string 'C:/web2.0/config/' (length=17)
1 => string 'C:/web2.0/js/' (length=13)
2 => string 'C:/web2.0/mydir/' (length=16)
3 => string 'C:/web2.0/myfile/' (length=17)