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)