PHP头条
热点:

2、使用Glob()查找文件

许多PHP函数具有长描述性的名称。然而可能会很难说出glob()函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是比scandir()函数更强大的版本,可以按照某种模式搜索文件。

  1. 以下为引用的内容:  
  2.  
  3. //getallphpfiles  
  4. $files=glob(‘*.php’);  
  5.  
  6. print_r($files);  
  7. /*outputlookslike:  
  8. Array  
  9. (  
  10. [0]=>phptest.php  
  11. [1]=>pi.php  
  12. [2]=>post_output.php  
  13. [3]=>test.php  
  14. )  
  15. */ 

你可以像这样获得多个文件:

  1. 以下为引用的内容:  
  2.  
  3. //getallphpfilesANDtxtfiles  
  4. $files=glob(‘*.{php,txt}’,GLOB_BRACE);  
  5.  
  6. print_r($files);  
  7. /*outputlookslike:  
  8. Array  
  9. (  
  10. [0]=>phptest.php  
  11. [1]=>pi.php  
  12. [2]=>post_output.php  
  13. [3]=>test.php  
  14. [4]=>log.txt  
  15. [5]=>test.txt  
  16. )  
  17. */ 

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

  1. 以下为引用的内容:  
  2.  
  3. $files=glob(‘../images/a*.jpg’);  
  4.  
  5. print_r($files);  
  6. /*outputlookslike:  
  7. Array  
  8. (  
  9. [0]=>../images/apple.jpg  
  10. [1]=>../images/art.jpg  
  11. )  
  12. */ 

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

  1. 以下为引用的内容:  
  2.  
  3. $files=glob(‘../images/a*.jpg’);  
  4.  
  5. //appliesthefunctiontoeacharrayelement  
  6. $files=array_map(‘realpath’,$files);  
  7.  
  8. print_r($files);  
  9. /*outputlookslike:  
  10. Array  
  11. (  
  12. [0]=>C:\wamp\www\images\apple.jpg  
  13. [1]=>C:\wamp\www\images\art.jpg  
  14. )  
  15. */ 


www.phpzy.comtrue/php/10568.htmlTechArticle2、使用Glob()查找文件 许多PHP函数具有长描述性的名称。然而可能会很难说出glob()函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是...

相关文章

相关频道:

PHP之友评论

今天推荐