douban5644 2013-11-18 10:36
浏览 37
已采纳

PHP RecursiveDirectoryIterator

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 folder.

For example I have the folders A and B.

In A, I have a list of files say, 1.txt, 2.php, 2.pdf, 3.doc, 3.pdf.

In B, I have 1.pdf, 1.jpg, 2.png.

I want my results to be like this:

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

How can this be done?

<?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 
    }} 
?>
  • 写回答

3条回答 默认 最新

  • donglu9134 2013-12-01 11:23
    关注
    //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 }
             }
    
                }
            }
    }
    
    
            }
    
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图