PHP implements method of traversing specific files in a directory

墨辰丷
Release: 2023-03-27 12:08:01
Original
1221 people have browsed it

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 details are as follows:

1. Use the glob method


foreach (glob("modules/*.php") as $filename) {
  echo $filename;
}
Copy after login


2. Use the ls command under Linux


##

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__);
Copy after login


Related recommendations:


PHP implementationTraverse the directoryMethods for specifying files

##Recursion under python

Traverse the directoryIntroduction to the methods of files

Introducing several methods of php

traversing directories and folders

The above is the detailed content of PHP implements method of traversing specific files in a directory. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template