How to quickly query files in a specified directory using php's glob function

高洛峰
Release: 2023-03-03 20:42:01
Original
1097 people have browsed it

The example in this article describes how PHP uses the glob function to quickly query files in a specified directory. Share it with everyone for your reference. The details are as follows:

php searches all files in the current directory, the code is as follows:

$array = glob('*.*'); 
print_r($array );  
 
/* 
Array 
( 
    [0] => 1.php 
    [1] => 10.php 
    [2] => 11.php 
    [3] => 2.asp 
    [4] => 3.asp 
    [5] => 4.aspx 
    [6] => 5.html 
    [7] => 6.php 
    [8] => 7.php 
    [9] => 8.php 
    [10] => 9.php 
) 
*/
Copy after login

Search for php files with .php results, the code is as follows:

$array = glob('*.php'); 
print_r($array );  
 
/* 
Array 
( 
    [0] => 1.php 
    [1] => 10.php 
    [2] => 11.php 
    [3] => 6.php 
    [4] => 7.php 
    [5] => 8.php 
    [6] => 9.php 
) 
*/
Copy after login

Search for files including php, aspx, the code is as follows:

$files = glob('*.{php,aspx}', GLOB_BRACE);  
print_r( $files ); 
/*  
Array 
( 
    [0] => 1.php 
    [1] => 10.php 
    [2] => 11.php 
    [3] => 6.php 
    [4] => 7.php 
    [5] => 8.php 
    [6] => 9.php 
    [7] => 4.aspx 
) 
*/
Copy after login

In the specified directory Search for php files that open with 1

$files = glob('../05-15/1*.php'); 
 
print_r($files);  
 
/* 
Array 
( 
    [0] => ../05-15/1.php 
    [1] => ../05-15/10.php 
    [2] => ../05-15/11.php 
) 
*/
Copy after login

and return the absolute path of the file. The code is as follows:

$files = array_map('realpath',$files);  
print_r($files);  
 
Array 
( 
    [0] => D:www.php.cn-15.php 
    [1] => D:www.php.cn-15.php 
    [2] => D:www.php.cn-15 .php 
)
Copy after login

The glob() function can do more powerful things than the scandir() function and can search for files according to a certain pattern.

I hope this article will be helpful to everyone’s PHP programming design.

For more related articles on how PHP uses the glob function to quickly query files in a specified directory, please pay attention to 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!