Home > Article > Backend Development > PHP implements method of traversing specified files in a directory
This article mainly introduces PHP's simple method of traversing specific files in a directory. It combines two simple examples to summarize and analyze PHP's traversal query operation skills for specified files in a directory. Friends who need it can refer to it
The example in this article describes how PHP simply implements traversing specific files in a directory. Share it with everyone for your reference, the details are as follows:
1. Use the glob method
foreach (glob("modules/*.php") as $filename) { echo $filename; }
2. Use Linux Under the ls command
function iterator($pattern, $__FILE__) { preg_match("/^(.+)\/[^\/]+$/", $__FILE__, $matches); $ls = `ls $matches[1]/$pattern`; $ls = explode("\n", $ls); array_pop($ls); // remove empty line ls always prints foreach ($ls as $inc) { echo($inc); } } iterator("modules/*.php",__FILE__);
The above is the detailed content of PHP implements method of traversing specified files in a directory. For more information, please follow other related articles on the PHP Chinese website!