Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does php read all file names in directories and subdirectories

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report