Using scandir() to find folders in a directory (PHP)

前端 未结 9 2098
Happy的楠姐
Happy的楠姐 2020-12-02 13:11

I am using this peice of code:

$target = \'extracted/\' . $name[0];  
$scan = scandir($target);

To scan the directory of a folder which is

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 14:03

    here is one function i used mostly to parse archives and directories

    function scan_full_dir($dir,$child=false){
        $dir_content_list = scandir($dir);
        foreach($dir_content_list as $value) 
        { 
            if($value === '.' || $value === '..') {continue;} 
            // check if we have file 
             if(is_file($dir.'/'.$value)) {
                echo '
    File Name --> '.$value; continue; } // check if we have directory if (is_dir($dir.'/'.$value)) { if(!$child){ echo '
    parent --> '.$value; }else{ echo '
    child of '.$child.' --> '.$value; } scan_full_dir($dir.'/'.$value,$value); } } }

    output sample

    parent --> fonts
    File Name --> standard
    parent --> steps
    child of steps --> pcb
    File Name --> .attrlist.sum
    File Name --> .profile.sum
    File Name --> .stephdr.sum
    File Name --> attrlist
    child of pcb --> netlists
    child of netlists --> cadnet
    File Name --> .netlist.sum
    File Name --> netlist
    File Name --> profile
    File Name --> stephdr
    

    // to scan dir

    scan_full_dir('path/of/myfolder');
    

    // to scan archive without opening it , supported formats : gz, zip, tar and bz files.

    scan_full_dir('phar://uploads/youarchive.zip);
    

提交回复
热议问题