Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get directory list?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 113
    Comment on it

    To get directory list in PHP we have define a function get_dirlist() in which we are passing a parameter which is the name of the directory. Inside this function we are executing the ls command with the help of exec() function. Using this we will get all the list of directories and files of the directory. The script is as follows:

    <?php 
    function get_dirlist($start_dir) { 
    exec("ls -R $start_dir",$f_list); 
    $dir_str = $start_dir; 
    $filelist[0] = $start_dir; $i = 1; 
    for ($count=0; $count < count($f_list); $count++) { 
       if ($f_list[$count] == "") { continue; } 
       if (substr($f_list[$count],strlen($f_list[$count])-1,1) == ":") { 
         $dir_str = substr($f_list[$count],0,strlen($f_list[$count])-1); 
         $filelist[$i] = $dir_str; 
         $i++; 
       } else { 
         $file_str = "$dir_str/$f_list[$count]"; 
         if (is_file($file_str)) { 
           $filelist[$i] = $file_str;   
           $i++; 
         } 
       } 
    } 
    return $filelist; 
    } 
    
    
    /* example */ 
    $start_dir ="../www/"; 
    $filelist = get_dirlist($start_dir); 
    for ($c=0; $c < count($filelist); $c++) { 
      echo $filelist[$c] . "<br>"; 
    }
    ?>
    

    Once you will execute that code you will find all the directory list.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: