Get the App
SLTechnology News&Howtos  ›  Development  › 

How does php read all file names in directories and subdirectories

Shulou Source: shulou.com Published: 2022-06-03 19:05:57 09月25日 Update

This article mainly explains "how to read all the file names under the directory and subdirectory by php". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how php reads all the file names under directories and subdirectories".

Generally speaking, there are many ways to read the file name under the directory in php. The simplest one is scandir. The specific code is as follows:

The copy code is as follows:

$dir= ". / caxa/"

$file=scandir ($dir)

Print_r ($file)

A slightly more complicated one comes from the php manual:

The copy code is as follows:

$dir = "/ etc/php5/"

/ / Open a known directory, and proceed to read its contents

If (is_dir ($dir)) {

If ($dh = opendir ($dir)) {

While (($file = readdir ($dh))! = = false) {

Echo "filename: $file: filetype:". Filetype ($dir. $file. "\ n"

} closedir ($dh)

}

}

These can only read the files in the current specified directory, but not the files in the subdirectory. Originally, I wrote a code to delete all directories in a loop, and I need to delete all files, including multiple layers, one by one. But you just need to read out the file name, which is a little more complicated, find one that works online, and there is an error in the original code, and change the reference & $data, as follows:

The copy code is as follows:

Function searchDir ($path,&$data) {

If (is_dir ($path)) {

$dp=dir ($path)

While ($file=$dp- > read ()) {

If ($fileboxes filled. Stories & $fileboxes filled.) {

SearchDir ($path.'/'.$file,$data)

}

}

$dp- > close ()

}

If (is_file ($path)) {

$data [] = $path

}

}

Function getDir ($dir) {

$data=array ()

SearchDir ($dir,$data)

Return $data

}

Print_r (getDir ('.')

At this point, I believe you have a deeper understanding of "how php reads all the file names under directories and subdirectories". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Code file directory file name subdirectory complex content learning original practical deeper generally speaking interest place multi-layer practical practical pair manual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Shulou Technology Huawei Apple