2、使用Glob()查找文件
许多PHP函数具有长描述性的名称。然而可能会很难说出glob()函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是比scandir()函数更强大的版本,可以按照某种模式搜索文件。
- 以下为引用的内容:
- //getallphpfiles
- $files=glob(‘*.php’);
- print_r($files);
- /*outputlookslike:
- Array
- (
- [0]=>phptest.php
- [1]=>pi.php
- [2]=>post_output.php
- [3]=>test.php
- )
- */
你可以像这样获得多个文件:
- 以下为引用的内容:
- //getallphpfilesANDtxtfiles
- $files=glob(‘*.{php,txt}’,GLOB_BRACE);
- print_r($files);
- /*outputlookslike:
- 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);
- /*outputlookslike:
- Array
- (
- [0]=>../images/apple.jpg
- [1]=>../images/art.jpg
- )
- */
如果你想获得每个文件的完整路径,你可以调用realpath()函数:
- 以下为引用的内容:
- $files=glob(‘../images/a*.jpg’);
- //appliesthefunctiontoeacharrayelement
- $files=array_map(‘realpath’,$files);
- print_r($files);
- /*outputlookslike:
- Array
- (
- [0]=>C:\wamp\www\images\apple.jpg
- [1]=>C:\wamp\www\images\art.jpg
- )
- */
PHP之友评论