PHP file_exists和通配符 [英] PHP file_exists and wildcard

查看:206
本文介绍了PHP file_exists和通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写PHP file_exists函数,以便它在目录中搜索具有任意扩展名的文件.例如,假设我知道一个文件名为"hello",但我不知道扩展名,我该如何编写一个函数来搜索名为hello.*的文件并返回该文件的名称?据我所知,file_exists只会搜索一个字符串.

Is there a way to write the PHP file_exists function so that it searches a directory for a file with an arbitrary extension. For instance, suppose I knew that a file were called "hello", but I didn't know the extension, how would I write a function that searched for a file called hello.* and returned the name of this file? As far as I can tell, file_exists will only search for a string.

谢谢.

推荐答案

您正在寻找 glob() 功能.

file_exists不执行任何类型的搜索:仅在知道文件名时才允许其知道文件是否存在.

file_exists doesn't do any kind of search : it only allows one to know whether a fle exists or not, when knowingf its name.

而且,在PHP> = 5.3的情况下,您可以使用新的 GlobIterator .

And, with PHP >= 5.3, you could use the new GlobIterator.


glob()为例,代码的以下部分:


As an example with glob(), the following portion of code :

$list = glob('temp*.php');
var_dump($list);

给我这个输出:

array
  0 => string 'temp-2.php' (length=10)
  1 => string 'temp.php' (length=8)


而这个:

$list = glob('te*-*');
var_dump($list);

是,有两个*;-)

请给我:

array
  0 => string 'temp-2.php' (length=10)
  1 => string 'test-1.php' (length=10)
  2 => string 'test-curl.php' (length=13)
  3 => string 'test-phing-1' (length=12)
  4 => string 'test-phpdoc' (length=11)

这篇关于PHP file_exists和通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆