Home > Backend Development > PHP Tutorial > PHP implements detailed explanation of traversing files and directories based on glob function

PHP implements detailed explanation of traversing files and directories based on glob function

墨辰丷
Release: 2023-03-28 21:22:01
Original
1363 people have browsed it

This article mainly introduces relevant information on the detailed explanation of PHP's use of the glob function to traverse files and directories. Friends in need can refer to the following

php glob() function returns the file name or directory that matches the specified pattern. Therefore, we can use the glob function to find files and traverse directories.

Function description: array glob (string $pattern [, int $flags])

Function: Find the file path that matches the pattern and return an array containing the matching files (directories) (Note: The file being checked must be in the server system and cannot be used for remote files)

Parameter description: The first parameter: matching pattern; the second optional parameter:

  1. GLOB_MARK - Add a slash to each returned item

  2. GLOB_NOSORT - Return the files in their original order of appearance in the directory (not sorted)

  3. GLOB_NOCHECK - Returns the pattern used to search if no files match

  4. GLOB_NOESCAPE - Backslash unescaped metacharacter

  5. GLOB_BRACE - Expand {a,b,c} to match 'a', 'b' or 'c'

  6. GLOB_ONLYDIR - Return only directory entries matching pattern

Example 1: Get all files and subdirectories in the specified directory


<?php
   $directories = glob("/tmp/*", GLOB_ONLYDIR);//获取/tmp/目录下的所有目录
   $complete = glob("/tmp/*");//获取/tmp/目录下的所有目录和文件
   $files = array_diff($directories, $complete);//获取/tmp/目录下的所有文件

   echo "Directories in /tmp/<BR>";
   foreach($directories as $val) {
     echo "$val<BR>\n";
   }
   echo "<BR>Files in /tmp/<BR>";
   foreach($files as $val) {
     echo "$val<BR>\n";
   }
?>
Copy after login


Example 2: You Are you still using opendir readdir to traverse files? You are really out!


<?php
$files = glob("dir/*.jpg");
foreach($files as $jpg){
  echo $jpg, "\n";
}
?>
Copy after login


Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of PHP XML and array conversion examples

php Generate signature And detailed explanation of signature verification

PHP Submit XML through POST, obtain XML, parse XML detailed explanation and examples_phpreal


The above is the detailed content of PHP implements detailed explanation of traversing files and directories based on glob function. 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