scandir 遍历文件目录

scandir 遍历文件目录

  • 该方法和glob 方法相比,可遍历含有特殊字符的文件夹名称,172.30.9.156[8000]_channel[1]_ITS 为例
$file_str = "./renlian/172.30.9.156[8000]_channel[1]_ITS/";
if(is_dir($file_str)){
    dump(getDirContent($file_str));
}

    function getDirContent($path){
        if(!is_dir($path)){
            return false;
        }

    //scandir方法
    $arr = array();
    $data = scandir($path);
    foreach ($data as $value){
        if($value != '.' && $value != '..'){
            $arr[] = $value;
        }
    }

    return $arr;
}

function dump($a){
    echo '<pre>';
    print_r($a);
    echo '</pre>';
}
exit;
  • opendir 方法如何实现?
$file_str = "./renlian/172.30.9.156[8000]_channel[1]_ITS/";
if(is_dir($file_str)){
    dump(getDirContent($file_str));
}

    function getDirContent($path){
        if(!is_dir($path)){
            return false;
        }

    $dir = opendir($path);
    $arr = array();
    while($content = readdir($dir)){
      if($content != '.' && $content != '..'){
        $arr[] = $content;
      }
    }

    closedir($dir);
    return $arr;
   }

function dump($a){
    echo '<pre>';
    print_r($a);
    echo '</pre>';
}

  • glob 方法如何实现,有特殊字符的文件夹情况如何处理?
原文地址:https://www.cnblogs.com/pansidong/p/12171409.html