dougou1127 2014-08-03 17:14
浏览 48
已采纳

使用scandir()使用php探索文件结构

I am new to php and trying to learn how to navigate a local file structure in for the format:

 -Folder
  -SubFolder
     -SubSubFolder
     -SubSubFolder
  -SubFolder
      -SubSubFolder
  ...

From another stackoverflow question I have been able to use this code using scandir():

<?php
$scan = scandir('Folder');

foreach($scan as $file)
{
    if (!is_dir($file))
    {
       $str = "Folder/".$file;
       echo $str;
    }
}
?>

This allows me to generate a list of strings of all the 'SubFolder' in my folder directory.

What I am trying to do is list all the 'SubSubFolder' in each 'SubFolder', so that I can create a string of the 'SubSubFolder' name in combination with its 'SubFolder' parent and add it to an array.

<?php
$scan = scandir('Folder');

foreach($scan as $file)
{
    if (!is_dir($file))
    {
        $str = "Folder/".$file;
        //echo $str;

        $scan2 = scandir($str);
        foreach($scan2 as $file){
            if (!is_dir($file))
            {
                echo "Folder/SubFolder/".$file;
            }
        }
    }
}
?>

This however isn't working, and I wasn't sure if it was because I cannot do consecutive scandir() or if I cannot use $file again.

  • 写回答

1条回答 默认 最新

  • dongzheng8463 2014-08-03 19:30
    关注

    There is probably a better solution, but hopefully the following will be of some help.

     <?php
    function getDirectory( $path = '.', $level = 0 ){
    
        $ignore = array( 'cgi-bin', '.', '..' );
        // Directories to ignore when listing output. Many hosts
        // will deny PHP access to the cgi-bin.
    
        $dh = @opendir( $path );
        // Open the directory to the handle $dh
    
        while( false !== ( $file = readdir( $dh ) ) ){
        // Loop through the directory
    
            if( !in_array( $file, $ignore ) ){
            // Check that this file is not to be ignored
    
                $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
                // Just to add spacing to the list, to better
                // show the directory tree.
    
                if( is_dir( "$path/$file" ) ){
                // Its a directory, so we need to keep reading down...
    
                    echo "<strong>$spaces -$file</strong><br />";
                    getDirectory( "$path/$file", ($level+1) );
                    // Re-call this same function but on a new directory.
                    // this is what makes function recursive.
    
                } else {
    
                    //To list folders names only and not the files within comment out the following line.
                    echo "$spaces $file<br />.";
                    // Just print out the filename
                }
            }
        }
        closedir( $dh );
        // Close the directory handle
    }
    getDirectory( "folder" );
    // Get the current directory
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常