Using this code:
$path = public_path().'/root';
$files = array();
$folders = array();
if (is_dir($path)) {
$dirhandle = opendir($path);
while (($dir = readdir($dirhandle)) !== false) {
if(is_dir($dir) && $dir!= "." && $dir!= "..") {
$folders[] = $dir;
}
else if(is_file($dir)) {
$files[] = $dir;
}
}
closedir();
}
return $folders;`
I can return the files inside the root folder but I can't return folders inside the root folder.
Inside the root folder, I have 3 folders and 4 files. Can you help me fix this error?
Based on my comment...
$contents = scandir('.'); // Working directory is current dir
foreach($contents as $key => $entry){
if(is_dir($entry)){
$directories[] = $entry;
} else {
$files[] = $entry;
}
}
echo('<pre>');
var_dump($directories, $files);
echo('</pre>');
Following code will fix your problem.
$path = public_path().'/root';
foreach (glob($path . '/*') as $item) {
if (is_dir($item)) {
$folders[] = basename($item);
} else {
$files[] = basename($item);
}
}
var_dump($folders);
var_dump($files);
more details about glob
http://php.net/manual/en/function.glob.php
you can use this code snippet
$fnf = scandir('.');
$result = array();
$i=$j=0;
foreach($fnf as $key => $val){
if(is_dir($val)){
if($val == '..' || $val == '.') //Remove if you want to current and root directory (i.e . & ..)
continue;
$result['dir'][$i] = $val;
$i++;
} else {
$result['files'][$j] = $val;
$j++;
}
}
echo '<pre>';
print_r($result);