6

我想在一个目录中的一组文件夹上做一个 RecursiveDirectoryIterator ,./temp然后根据文件夹的名称列出每个文件夹中的文件。

例如,我有文件夹AB.

在 A 中,我有一个文件列表,例如, 1.txt, 2.php, 2.pdf, .3.doc3.pdf

在 B 中,我有1.pdf, 1.jpg, 2.png.

我希望我的结果是这样的:

A => List of files in A
B => List of files in B

如何才能做到这一点?

<?php 
$scan_it = new RecursiveDirectoryIterator("./temp"); 
foreach(new RecursiveIteratorIterator($scan_it) as $file =>$key) { 
    $filetypes = array("pdf"); 
    $filetype = pathinfo($file, PATHINFO_EXTENSION); 
    if (in_array(strtolower($filetype), $filetypes)) { 
        $dlist=basename($file); //sort 
?> 
<ul>
    <li>
        <?php echo substr(dirname($file),11);?>
    </li> 
    <li>
        <a href="<?php echo $file;?>"><?php echo basename($file);?></a>
    </li>
</ul> 
<?php 
    }} 
?>
4

3 回答 3

9

使用RecursiveDirectoryIterator和的组合RecursiveIteratorIterator遍历所有子目录。

下面的代码片段将满足您的要求,尽管它仅限于创建一个深度为一个数组的数组......这是为了避免在已经令人困惑的程序大脑小片段中陷入递归混乱。

$array = array();

foreach ($iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator("./temp", 
        RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST) as $item) {
    // Note SELF_FIRST, so array keys are in place before values are pushed.

        $subPath = $iterator->getSubPathName();
            if($item->isDir()) {
                // Create a new array key of the current directory name.
                $array[$subPath] = array();
            }
            else {
                // Add a new element to the array of the current file name.
                $array[$subPath][] = $subPath;
            }
    }
}
于 2013-11-28T14:25:10.203 回答
0
//Managed to put this together and it somehow works for me. If you have other options please provide. Thanks

$path='./temp';
$dir  = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);

    foreach ($files as $file=>$mykey) {
        if(is_dir($file)) {

$directory = $file;
$mydir = new RecursiveIteratorIterator(new RecursiveRegexIterator(new RecursiveDirectoryIterator($directory,RecursiveDirectoryIterator::FOLLOW_SYMLINKS), 
// match both pdf file extensions and directories
                '#(?<!/)\.pdf$|^[^\.]*$#i'),true);
    foreach ($mydir as $myfile=>$mykey) {
    $result[] = $myfile.'<br />';

    $filetypes = array("pdf");
    $filetype = pathinfo($myfile, PATHINFO_EXTENSION);
    if (in_array(strtolower($filetype), $filetypes)) {


// output all matches substr(dirname($file),11)

?>


    <div><h3<?php echo count($result);?>>
    <span><?php echo  substr(PHP_EOL . $directory . PHP_EOL . PHP_EOL,13);?></span></h3></div>

    <?php if (!empty($mydir)) {
    foreach ($mydir as $d) {
    if (strpos($d->getFilename(), '.pdf') !== FALSE) {?>
    <div><ul><li><a href="<?php echo $d;?>"><?php echo basename($d->getPath() . DIRECTORY_SEPARATOR . $d->getFilename() . PHP_EOL);?></a></li></ul></div>
        <?php }
         }

            }
        }
}


        }



}
于 2013-12-01T11:23:50.900 回答
-1

我认为您需要的是scandir功能。该链接向您显示了此功能的文档。

于 2013-11-18T10:39:37.233 回答