I have a system that stores user comments in its own individual json file. I use scandir(); on the directory which gets all files and folders, but how do I limit it to json files, I don’t want other files such as “.” and “..” in the array because I need an accurate count.
I checked out the info on php.net but couldn’t figure it out, perhaps you know of a resource you can point me toward, or which function to use.
There are no stream context parameters that will help you filter out the types of files.
Assuming that your JSON files are saved with the
.jsonextension, you just have to filter out the array based on the file extensions.You can use
readdir()to build a list of files, or simply loop over the results you get fromscandirand create a new array from that.Here is an example using
readdir: