本文執行個體講述了php使用glob函數快速查詢指定目錄檔案的方法。分享給大家供大家參考。具體如下:
php搜尋目前的目錄所有檔案,代碼如下:
複製代碼 代碼如下:
$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
)
*/
搜尋以.php結果的php檔案,代碼如下:
複製代碼 代碼如下:
$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
)
*/
搜尋包括有php,aspx 檔案,代碼如下:
複製代碼 代碼如下:
$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
)
*/
在指定目錄搜尋以1開的php檔案
複製代碼 代碼如下:
$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
)
*/
返迴文件的絕對路徑,代碼如下:
複製代碼 代碼如下:
$files = array_map('realpath',$files);
print_r($files);
Array
(
[0] => D:www.jb51.net-15.php
[1] => D:www.jb51.net-15.php
[2] => D:www.jb51.net-15 .php
)
glob()函數能做的事比scandir()函數更強大,可以按照某種模式搜尋檔案。
希望本文所述對大家的PHP程式設計有所協助。