There are a number of ways to find the files in a directory with PHP. I've covered opendir() readdir() and closedir() and glob() in previous posts and now look at scandir().
Example directory
The examples below look at a directory with the following, the same example directory as used in the read through directory and glob posts:
Simple example
The following example reads all the files in the directory at /path/to/directory into an array using scandir() and then echos out the array:
The output from the above using the example directory is as follows:
By default scandir() sorts the files into alphabetical order. To reverse the order pass a non-zero value as the second parameter like so:
will the resulting output:
Excluding the . and .. directories
To exclude the . and .. directory aliases you can do this:
This saves having to conditionally check for these when looping through the array.
I am really enjoying reading your well-written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
ReplyDeleteDot Net Course in Chennai | Best Dot Net Training Institute in Chennai
Testing Courses in Chennai | Best Software Testing Training Institute in Chennai With Placement
Core Java Training in Chennai | Java Certification in Chennai | Java Training Institute in Chennai
PHP Training Institute in Chennai | Best PHP Course in Chennai | Best PHP Training Institutes