44 PHP使用Glob()函数查找文件 - 雪炭网

PHP使用Glob()函数查找文件2015-06-16 11:42:14

( 3人已投票,[高质量] )
分享:
31.3K

在PHP中,有许多函数都具有长描述性的名称,却很少有人知道glob()的职责所能,除非你已经通过多次使用并熟悉了它。其实你可以把它看做比scandir()更强大的版本,主要是以某种模式搜索文件。

以下为引用的内容:


// get all php files
$files = glob('*.php');
print_r($files);
/* output looks like:
Array
(
  [0] => phptest.php
  [1] => pi.php
  [2] => post_output.php
  [3] => test.php
)
*/


这样就轻松的得到了文件列表了。

但是用Glob()怎么做呢,见如下内容:


// get all php files AND txt files
$files = glob('*.{php,txt}', GLOB_BRACE);
print_r($files);
/* output looks like:
Array
(
  [0] => phptest.php
  [1] => pi.php
  [2] => post_output.php
  [3] => test.php
  [4] => log.txt
  [5] => test.txt
)
*/


请注意,这些文件其实是可以返回一个路径,这取决于查询条件:

以下为引用的内容:


$files = glob('../images/a*.jpg');
print_r($files);
/* output looks like:
Array
(
  [0] => ../images/apple.jpg
  [1] => ../images/art.jpg
)
*/


如果你想获得每个文件的完整路径,你可以调用 realpath() 函数:

以下为引用的内容:


$files = glob('../images/a*.jpg');
// applies the function to each array element
$files = array_map('realpath',$files);
print_r($files);
/* output looks like:
Array
(
  [0] => C:\wamp\www\images\apple.jpg
  [1] => C:\wamp\www\images\art.jpg
)
*/







头像

snowcoal
  • PHP
  • Glob()
  • 查找文件

本文标签:

PHPGlob()查找文件

收藏到我的私密空间

标题:PHP使用Glob()函数查找文件

作者:花花世界

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~