Glob your directory for list of files in php
The glob function can be your greatest ally in simplifying tasks of getting a list of files from a directory. Lets say you need a list of all the jpg files in your image directory. You can go about this in multiple ways, one of which you could use opendir/readdir to look at all the files in the directory, then parse the filenames as strings to see if they end in ".jpg".
There is an easier way to get this work done, use the glob function. To get an array of all the files ending in jpg you can use a pattern and send glob out to do the dirty work: glob('images/*.jpg') will get you a list of those jpg files.
There is a secondary parameter to glob that can help you out with other tasks. Say that instead of jpg files you want to get all the directories that are in the images directory, glob('images/*', GLOB_ONLYDIR) will do the trick. This extra parameter tells glob to only look for directories.