0

I feel like I'm missing something obvious here. I have a class as follows:

<?php

class files
{
public function getDirectoryList($directory)
{
    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
    while($file = readdir($handler)) 
    {

        // if the file isn't this directory or the parent, add it to the results.
        if($file != "." && $file != "..")
        {
            $results[] = $file;
        }
    }

    // tidy up: close the handler
    closedir($handler);

    //done
    return $results;
}
}

?>

Now I have the class included into another file and I'm doing the following:

$fileListing = new files();
$fileListing->getDirectoryList('education');

I'm not getting any results back. If I take the function out of the class and put it in this file, I can get results by doing:

$fileListing = getDirectoryList('education');
5
  • 1
    Are you returning the class method call into a variable, as you are in the second example? Aug 26, 2012 at 16:31
  • 1
    Nothing wrong in your code, check what var_dump( $fileListing->getDirectoryList('education') ) tells you.
    – moonwave99
    Aug 26, 2012 at 16:32
  • if you want to print out the result, use 'echo $results' in your function, otherwise assign to variable, like suggested by @JaredFarrish Aug 26, 2012 at 16:34
  • Have you tried passing absolute paths in the two versions of the code, just to make sure this is not related to relative paths? Aug 26, 2012 at 16:39
  • Sheesh.... I completely spaced on assigning it to a variable. Using $myfiles = $fileListing->getDirectoryList('education'); worked. I just wasn't seeing that. Thanks Jared and Dirk.
    – Nick Bair
    Aug 26, 2012 at 16:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.