Search code examples
phpreaddir

read dir error (readdir(): supplied argument is not a valid Directory resource )


tryin to read dir content with readdir($myDirectory), but i getting error:

readdir(): supplied argument is not a valid Directory resource 

i checked with is_dir($myDirectory) is it directory or not, and yes, it is directory.

so, why i can not read dir? is it permissions problem?

just to mention, it's all on win xp box, not linux.

tnx in adv for your help!


Solution

  • is_dir() needs a path. readdir() needs a resource. The resource needed by readdir() is retrieved thanks to the opendir() method.

    dir_handle (the parameter)

    The directory handle resource previously opened with opendir(). If the directory handle is not specified, the last link opened by opendir() is assumed.

    For example :

    <?php
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                echo "$file\n";
            }
        }
        closedir($handle);
    }
    ?>
    

    Resources :