-1

So, I have this function and for some reason, it only returns dots, while anywhere else it returns what it should

function scan($dir){
    $open = opendir($dir);
    $Name = readdir($open);
    return $Name;
}

scan('/home/user/scan/scan-test2');

I used the exact same way of opening directories in another function and for some reason there it reads everything that is inside scan-test2.

In scan-test2 there is a folder with 4 text files in it

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • Why not use [scandir](http://php.net/manual/en/function.scandir.php) – IsThisJavascript Jan 18 '18 at 10:32
  • @IsThisJavascript scandir returns nothing or NULL with the warning " scandir() expects parameter 1 to be a valid path, resource given" – SoManyQuestions Jan 18 '18 at 10:34
  • It sounds like you're passing the result of `opendir` into `scandir` - you just need to provide the path. `readdir` only returns a single entry from a directory with each call, which is why this won't be working. – iainn Jan 18 '18 at 10:37
  • Your function only ever returns the first entry found in any case ... You need a _loop_ if you want all entries. – CBroe Jan 18 '18 at 10:38
  • thank you all... didn't expect the answer to be that simple – SoManyQuestions Jan 18 '18 at 10:41

1 Answers1

0

Use the scandir() PHP's function as this:

$files = scandir($path);
unset($files[0]); // remove .
unset($files[1]); // remove ..
user2342558
  • 5,567
  • 5
  • 33
  • 54