I’ve got this weird problem where PHP can see most of the files in a directory, but not all of them. They all have the same owner, group, and permissions. I’m using PHP’s scandir to get a list of files in a directory, and have also tried readdir and glob with the same results.

Doing a specific test with file_exists on one of the files that PHP lists using scandir and one it misses, I’ve found that when I use a relative path it can’t see the missing file. However if I use an absolute path then it can see both of the files.

But using scandir or glob, even with an absolute path it still can’t see all the files (no difference from using a relative path).

The setup is this - I have a directory with the PHP files in. Within this directory is an ‘Img’ directory, which is a symlink to a directory on block storage. The PHP script then uses scandir to get a list of files within a sub-dir of the 'Img’ dir.

Anyone got any ideas?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

1 answer

Hi @djeyewater,

Can you provide us with the path of scandir? Scandir does not work recursively. It only scans the path input into it. If you have more than one subdirectory in your Img folder, it would explain why one file is shown and another is not.

As for the relative and absolute paths, if you are executing scandir from a different directory than the originally intended one, it would explain why it doesn’t work.


Submit an Answer