PHP使用Glob()函数查找文件2015-06-16 11:42:14
( 3人已投票,[高质量] )
在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 ) */
版权声明:
作者信息(Author) :雪炭网 : 花花世界
原文链接(Hyperlink):https://snowcoal.com/article/2090.html
原创内容,尊重版权,转载需注明出处;商业及其他特殊用途转载需原作者同意。
原文链接(Hyperlink):https://snowcoal.com/article/2090.html
原创内容,尊重版权,转载需注明出处;商业及其他特殊用途转载需原作者同意。
下一篇:PHP获取内存使用情况