Home  >  Article  >  Backend Development  >  Detailed example of php reading all files in a folder

Detailed example of php reading all files in a folder

零下一度
零下一度Original
2017-06-17 10:45:232471browse

This article mainly introduces the method of php to read out all the files in a folder and its subfolders, involving phprecursion and file path related operation skills. Friends in need can refer to the following

The example in this article describes how PHP reads out all the files in a folder and its subfolders. Share it with everyone for your reference. The details are as follows:

Today’s requirement is to read out all the files under this folder in a folder, including of course all the subfolders under this folder. Of course There are many tutorials on the Internet, but in order to understand it more deeply, it is better to write it yourself. The code is as follows:


$path = './use';
$result = scanFile($path);
function scanFile($path) {
  global $result;
  $files = scandir($path);
  foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
      if (is_dir($path . '/' . $file)) {
        scanFile($path . '/' . $file);
      } else {
        $result[] = basename($file);
      }
    }
  }
  return $result;
}

The above is the detailed content of Detailed example of php reading all files in a folder. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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