Home  >  Article  >  Backend Development  >  Sample code for how to read out all the files in a folder and its subfolders in PHP

Sample code for how to read out all the files in a folder and its subfolders in PHP

怪我咯
怪我咯Original
2017-07-05 10:22:131289browse

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 Sample code for how to read out all the files in a folder and its subfolders in PHP. 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