PHP RecursiveDirectoryIterator

前端 未结 3 1769
一向
一向 2020-12-11 18:20

I want to do a RecursiveDirectoryIterator on a set of folders in a directory, say ./temp and then list the files in each folder according to the name of the fol

相关标签:
3条回答
  • 2020-12-11 19:01
    //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 }
             }
    
                }
            }
    }
    
    
            }
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-11 19:03

    Use a combination of RecursiveDirectoryIterator and RecursiveIteratorIterator to iterate through all subdirectories.

    The snippet below will do what you require, although it is limited to only creating an array one array deep... this is to avoid getting into a recursive mess, within an already-confusing-to-procedural-brains small snippet.

    $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;
                }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 19:20

    I think what you need is the scandir function. The link shows you the documentation of this function.

    0 讨论(0)
提交回复
热议问题