Home > Backend Development > PHP Tutorial > Example of how to read all files in a folder in php

Example of how to read all files in a folder in php

陈政宽~
Release: 2023-03-11 19:34:02
Original
3754 people have browsed it

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

The above is the detailed content of Example of how to read all files in a folder in php. 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