0

Can someone help me out? I cannot see it. The function below is not returning the array. The print_r($list)(above the return) prints the array on the screen. But the print_r($files)returns an empty array....

function listFolderFiles($dir){
    $ffs = scandir($dir);
    $i = 0;
    $list = array();
    foreach ( $ffs as $ff ){
        if ( $ff != '.' && $ff != '..' ){
            if ( strlen($ff)>=5 ) {
                if ( substr($ff, -4) == '.mp4' ) {
                    $value = $dir.'/'.$ff;
                    $list[] = $value;
                }
            }
            if( is_dir($dir.'/'.$ff) )
                    listFolderFiles($dir.'/'.$ff);
        }
    }

    print_r($list);  // Returns the full array with values
    return $list;
}
$files = listFolderFiles($_POST['path']);
print_r($files) // Returns an empty array..... :(:(
5
  • 1
    what is that ? ($_POST('path'))? a typo? $_POST['path'] Commented Oct 20, 2014 at 7:05
  • My mistake, made a typo when I posted the question. Was written right in the PHP file though, so still not working Commented Oct 20, 2014 at 7:07
  • I notice that the $list returns two times an array..... One filled and one empty. Maybe it runes the listFolderFiles too many times? Commented Oct 20, 2014 at 7:09
  • So, when you run it like this, then both print_rs are called, and one prints out the desired output, and one does not? Is it possible, that the POST value doesn't arrive/is not correct? Commented Oct 20, 2014 at 7:09
  • My guess is that it finds something in the if( is_dir($dir.'/'.$ff) ) part. But there you are ignoring its return value. Commented Oct 20, 2014 at 7:11

1 Answer 1

3

Your recursion call doesn't handle the returned array:

listFolderFiles($dir.'/'.$ff);

You need to merge the array here:

$list = array_merge($list, listFolderFiles($dir.'/'.$ff) );
Sign up to request clarification or add additional context in comments.

1 Comment

@ErikVandeVen I updated the answer, try it like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.