scandir file directory traversal

scandir file directory traversal

  • The method and file glob method compared to traverse a folder name contains special characters, 172.30.9.156 [8000] _channel [1] _ITS an example
$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;
  • How opendir way to achieve?
$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>';
}
  • How glob method to achieve, there are special characters in the folder how to handle?

Guess you like

Origin www.cnblogs.com/pansidong/p/12171409.html