I am trying to make an ARRAY
of files and subfolder inside a specific folder. I got success in getting a list of files and subfolders but when I try kept this data in an ARRAY
it only show result as expected within LOOP
. Once LOOP
finished it return EMPTY
Here is full code
<?php
function dirToOptions($path = '../data/vid/', $level = 0) {
$items = scandir($path);
$vidCount = 0; $thumbCount = 0;
foreach($items as $item) {
// ignore items strating with a dot (= hidden or nav)
if (strpos($item, '.') === 0) {
continue;
}
$fullPath = $path . DIRECTORY_SEPARATOR . $item;
// add some whitespace to better mimic the file structure
$item = str_repeat(' ', $level * 3) . $item;
// file
if (is_file($fullPath)) {
$rrInt = (int)$level-1;
if($rrInt == 0){
$vid['videos'][] = array('name'=>$item, 'link'=>$fullPath);
echo "<a href='$fullPath'><i for='$rrInt'>$item</i></a><br />";
}elseif($rrInt == 1){
$thumb['thumbs'][] = array('name'=>$item, 'link'=>$fullPath);
echo "<a href='$fullPath'><i for='$rrInt'>$item</i></a><br />";
var_dump($thumb); //SHOW RESULT HERE
}
}
// dir
else if (is_dir($fullPath)) {
// immediatly close the optgroup to prevent (invalid) nested optgroups
echo "<b label='$level'>$item</b><br />";
// recursive call to self to add the subitems
dirToOptions($fullPath, $level + 1);
}
}
}
$array = array(); $vid = array(); $thumb = array();
echo '<div>';
dirToOptions();
echo '</div>';
var_dump($vid); // RETURN EMPTY
?>
Thanks to all for support but at last I prefer to use
GLOBAL VARS