php blob 函数快速查询指定目录文件实例

php搜索当前目录所有文件,代码如下:

  1. $array = glob('*.*');
  2. print_r($array );
  3. /*
  4. Array
  5. (
  6. [0] => 1.php
  7. [1] => 10.php
  8. [2] => 11.php
  9. [3] => 2.asp
  10. [4] => 3.asp
  11. [5] => 4.aspx
  12. [6] => 5.html
  13. [7] => 6.php
  14. [8] => 7.php
  15. [9] => 8.php
  16. [10] => 9.php
  17. )
  18. */

搜索以.php结果的php文件,代码如下:

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

搜索包括有php,aspx 文件,代码如下:

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

在指定目录搜索以1开的php文件

  1. $files = glob('../05-15/1*.php');
  2. print_r($files);
  3. /*
  4. Array
  5. (
  6. [0] => ../05-15/1.php
  7. [1] => ../05-15/10.php
  8. [2] => ../05-15/11.php
  9. )
  10. */

返回文件的绝对路径,代码如下:

  1. $files = array_map('realpath',$files);
  2. print_r($files);
  3. Array
  4. (
  5. [0] => D:wwwwww.phpfensi.com-15.php
  6. [1] => D:wwwwww.phpfensi.com-15.php
  7. [2] => D:wwwwww.phpfensi.com-15 .php
  8. )

glob() 函数能做的事比scandir() 函数更强大,可以按照某种模式搜索文件.