我现在有一个站点来显示所有目录和子目录(在MAC os上),所以我在这里有代码。
$Directory = new RecursiveDirectoryIterator('/');
$Iterator = new RecursiveIteratorIterator($Directory);
foreach($Iterator as $name => $object){
echo "$name<br/>";
}因此,它将回显目录的所有真实路径(当前正在运行windows环境)。
C:\xampp\apache\bin
C:\xampp\apache\bin\.
C:\xampp\apache\bin\..
C:\xampp\apache\bin\ab.exe
C:\xampp\apache\bin\abs.exe
C:\myfolder\mysubfolder
C:\myfolder\mysubfolder\.
C:\myfolder\mysubfolder\..
C:\myfolder\mysubfolder\anotherfile.php
C:\myfolder\mysubfolder\another_folder
C:\myfolder\mysubfolder\another_folder\.
C:\myfolder\mysubfolder\another_folder\..
C:\myfolder\mysubfolder\another_folder\file.txt我想要做的是,在列表中显示如下内容:
C:\
xampp\
apache\
bin\
ab.exe
abs.exe
myfolder\
mysubfolder\
anotherfile.php\
another_folder\
file.txt是否有可能像我在上面放置的那样,用数组来格式化数据,而不是完整的路径?我不知道如何使用“爆炸”并将它们分组成新的数组.?
你好,我试过以下代码:
foreach($objects as $name => $object){
$newname=explode('\\', $name);
print_r($newname);
}它给了我:
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin}
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]-> . }
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]=>.
[5]=>. . }
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]=>ab.exe }
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]=>abs.exe }现在,我不知道如何循环它们,这样它们就会在组中回音。
xampp
apache
bin
ab.exe
abs.exe谢谢
发布于 2014-03-13 11:45:26
DirectoryIterator (+递归函数)可以为您做到这一点。
$resutl = RecursiveDirectoryIterator(new DirectoryIterator('/path/to/dir'));
function RecursiveDirectoryIterator(DirectoryIterator $path)
{
$data = array();
foreach ($path as $node){
if ($node->isDir() && !$node->isDot()){
$data[$node->getFilename()] = RecursiveDirectoryIterator(new DirectoryIterator($node->getPathname()));
}
elseif ($node->isFile()){
$data[] = $node->getFilename();
}
}
return $data;
}发布于 2014-03-13 11:56:14
这里是非递归版本,它的工作方式是首先对迭代结果进行排序,然后再分割路径数量。
<?
$folder = ".";
$Directory = new RecursiveDirectoryIterator($base);
$Iterator = new RecursiveIteratorIterator($Directory);
$SortedIterator = new SortedIterator($Iterator);
foreach($SortedIterator as $name){
// echo $name."\n";
$path = explode(DIRECTORY_SEPARATOR, $name);
$file = array_pop($path);
// it's a directory
if ($file == ".")
{
$file = array_pop($path) . DIRECTORY_SEPARATOR;
}
if ($file === "..")
{
continue;
}
$pathcount = count($path);
echo str_repeat(" ", $pathcount).$file."\n";
}
# Taken from example: http://stackoverflow.com/questions/2930405/sort-directory-listing-using-recursivedirectoryiterator
class SortedIterator extends SplHeap
{
public function __construct(Iterator $iterator)
{
foreach ($iterator as $item) {
$this->insert($item);
}
}
public function compare($b,$a)
{
return strcmp($a->getRealpath(), $b->getRealpath());
}
}示例输出:
./
ChangeLog
LICENSE.BSD
README.md
bin/
phantomjs
example.txt
examples/
arguments.coffee
arguments.js
child_process-examples.coffee
child_process-examples.js
colorwheel.coffee
colorwheel.js
colorwheel.png
countdown.coffee
countdown.js
detectsniff.coffee
detectsniff.js
direction.coffee
direction.js
echoToFile.coffee
echoToFile.js
rasterize.js
third-party.txthttps://stackoverflow.com/questions/22376724
复制相似问题