phpdirectoryscandir

What is wrong in this way of using PHP scandir() function?


I have been writing a simple PHP script for reading all files in a directory and show them but it show Unknown files, my files in images folder just two images but scandir() returning to me 4 result that first is "." (point) and second is ".." (double point). these results are from where?

This is my PHP function :

    $carpet_images=scandir('./images');

    print_r($carpet_images);

and this is result:

Array
(
[0] => .
[1] => ..
[2] => 1562475195-1796949251.jpg
[3] => 5f931b6fdbbdc8bfbd164f4bb08334ec_zzz-1.jpg
)

Solution

  • . is the current directory, .. is the parent directory. You can filter those out like this:

    $directories = scandir("./images");
    
    $carpet_images = array_filter($directories, function($var) {
        return !in_array($var, ['.', '..']);
    });