php使用glob函數(shù)快速查詢指定目錄文件的方法
更新時間:2014年11月15日 14:53:38 投稿:shichen2014
這篇文章主要介紹了php使用glob函數(shù)快速查詢指定目錄文件的方法,可實現(xiàn)快速搜索指定格式文件的功能,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php使用glob函數(shù)快速查詢指定目錄文件的方法。分享給大家供大家參考。具體如下:
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
)
*/
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結(jié)果的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
)
*/
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
)
*/
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
)
*/
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.dbjr.com.cn-15.php
[1] => D:www.dbjr.com.cn-15.php
[2] => D:www.dbjr.com.cn-15 .php
)
print_r($files);
Array
(
[0] => D:www.dbjr.com.cn-15.php
[1] => D:www.dbjr.com.cn-15.php
[2] => D:www.dbjr.com.cn-15 .php
)
glob()函數(shù)能做的事比scandir()函數(shù)更強大,可以按照某種模式搜索文件。
希望本文所述對大家的PHP程序設計有所幫助。
相關(guān)文章
使用PHPStorm+XDebug搭建單步調(diào)試環(huán)境
由于 PhpStorm 不像 Zend 公司為 Zend Studio 那樣配套集成了很多開發(fā)部件,包括解釋器、調(diào)試器、虛擬機、服務器、開發(fā)框架等等。因此,配置 PhpStorm 開發(fā)環(huán)境相對較繁瑣,有很多需要注意的地方。2017-11-11php使用function_exists判斷函數(shù)可用的方法
這篇文章主要介紹了php使用function_exists判斷函數(shù)可用的方法,通過一個圖像處理函數(shù)中使用function_exists函數(shù)判斷并輸出來實現(xiàn)函數(shù)存在判斷與流程靈活控制的功能,具有很好的借鑒價值,需要的朋友可以參考下2014-11-11百度工程師講PHP函數(shù)的實現(xiàn)原理及性能分析(三)
這篇文章主要介紹了百度工程師講PHP函數(shù)的實現(xiàn)原理及性能分析(三),本文講解了常用php函數(shù)實現(xiàn)及介紹,并作了總結(jié)及建議,需要的朋友可以參考下2015-05-05PHP JSON出錯:Cannot use object of type stdClass as array解決方法
這篇文章主要介紹了PHP JSON出錯:Cannot use object of type stdClass as array解決方法,需要的朋友可以參考下2014-08-08