0
$files = array();
    
function listFolderFiles($dir) {
    $ffs = scandir($dir);
    echo '<ol>';
        foreach ($ffs as $ff) {
            if ($ff != '.' && $ff != '..') {
                if (is_dir($dir . '/' . $ff))
                    listFolderFiles($dir . '/' . $ff);
                else
                    echo '<li>' . $ff;
                
                array_push(
                    $files,
                    array(
                        'file' => $dir . '/' . $ff,
                        'hash' => hash_file('md5', $dir . '/' . $ff)
                    )
                );
                echo '</li>';
            } 
        }
    echo '</ol>';
}
    
listFolderFiles('/var/www');
var_dump($files);

Why am I getting empty output here?

array(0) { };
6
  • Do you see the "echo"? Commented Apr 17, 2014 at 8:15
  • yes.it's working nicely Commented Apr 17, 2014 at 8:15
  • 1
    your array is declared outside the function Commented Apr 17, 2014 at 8:16
  • 1
    Aren't you getting warnings about accessing an uninitialized variable? If not, make sure you have error reporting turned up fully. Commented Apr 17, 2014 at 8:17
  • 1
    do not use globals, it's bad practice Commented Apr 17, 2014 at 8:18

4 Answers 4

6

This is a scope issue. Your $files array variable is outside of your function.

You need to pass that as a parameter as a reference (Tipped by @Barmar ;) ) to it..

The right way..

function listFolderFiles($dir,&$files){

and call it like.. listFolderFiles('/var/www',$files);

You are passing it as a reference because you are not returning the array , so whatever changes done inside the function to the array will remain there itself. So you need to add reference as shown in the method signature. This will modify the original array itself.

Sign up to request clarification or add additional context in comments.

5 Comments

It needs to be passed by reference.
This answer worked after adding the reference.Why is that & mean ? @Barmar
@samitha, I have explained in the answer. Please see.
@samitha the reference (&) means that it changes the variable after processing the function
@samitha I'd suggest to read much more the PHP manual: php.net/manual/en/language.references.pass.php
3

You can pass a variable by reference, as in Shankar's answer. Or you can have the function return a result:

$files = listFolderFiles($dir);    

function listFolderFiles($dir){
    $files = array();
    $ffs = scandir($dir);
    echo '<ol>';

    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            else echo '<li>'.$ff;

            array_push($files, array('file' => $dir.'/'.$ff, 'hash' => hash_file('md5', $dir.'/'.$ff)));
            echo '</li>';
        } 
    }
    echo '</ol>';
    return $files;
}

Comments

0

For you purposes here, it may be better to declare the files variable inside the function itself, and return it at the end. This way, the function will be re-usable:

function listFolderFiles($dir){
    $files = array();
    $ffs = scandir($dir);
    echo '<ol>';

    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            else echo '<li>'.$ff;

            array_push($files, array('file' => $dir.'/'.$ff, 'hash' => hash_file('md5', $dir.'/'.$ff)));
            echo '</li>';
        } 
    }
    echo '</ol>';
    return $files;
}

$files = listFolderFiles('/var/www');
var_dump($files);

Comments

-1

you can use globals

function listFolderFiles($dir){

global $files; // global var

    $ffs = scandir($dir);

    echo '<ol>';
    foreach($ffs as $ff){

        if($ff != '.' && $ff != '..'){

            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            else echo '<li>'.$ff;
            array_push($files, array('file' => $dir.'/'.$ff, 'hash' => hash_file('md5', $dir.'/'.$ff)));
            echo '</li>';

        }

8 Comments

bad idea, do not follow it
@royal why, i used it jst yestiday to solve a scope issue i had. is there a better way?
@longJOURNEY See Shankar's answer.
Although it would work, it is nearly always bad practice to use globals. Why define something to be available everywhere when you just need it at a much smaller scope?
it resolves the scope issue for the current need, but leaves the code leaning on globals. A well structured app will not lean on globals. Better way is Shankar's answer. And best is to use objects and instance variables which will change i.e. private $_files = array(); public function listFolderFiles($dir) { array_push($this->_files, array(....)); }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.