php Quick Query Method of Specified Directory File Using glob Function

  • 2021-08-03 09:49:04
  • OfStack

This article illustrates how php uses glob functions to quickly query specified directory files. Share it for your reference. The details are as follows:

php searches for all files in the current directory with the following code:

$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
)
*/

Search for php files with. php results, 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
)
*/

The search includes php and aspx files with the following code:
$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
)
*/

Search the specified directory for php files opened at 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
)
*/

Returns the absolute path of the file with the following code:
$files = array_map('realpath',$files);  
print_r($files); 
 
Array
(
    [0] => D:www.ofstack.com-15.php
    [1] => D:www.ofstack.com-15.php
    [2] => D:www.ofstack.com-15 .php
)

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

I hope this article is helpful to everyone's PHP programming.


Related articles: