PHP file_exists 和通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2746364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP file_exists and wildcard
提问by bsamek
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.
有没有办法编写 PHP file_exists 函数,以便它在目录中搜索具有任意扩展名的文件。例如,假设我知道一个文件名为“hello”,但我不知道扩展名,我将如何编写一个函数来搜索名为 hello.* 的文件并返回该文件的名称?据我所知, file_exists 只会搜索一个字符串。
Thanks.
谢谢。
回答by Pascal MARTIN
You're looking for the glob()function.
您正在寻找该glob()功能。
file_existsdoesn't do any kind of search : it only allows one to know whether a fle exists or not, when knowingf its name.
file_exists不进行任何类型的搜索:当知道文件名称时,它只允许人们知道文件是否存在。
And, with PHP >= 5.3, you could use the new GlobIterator.
而且,对于 PHP >= 5.3,您可以使用新的GlobIterator.
As an example with glob(), the following portion of code :
以 为例glob(),代码的以下部分:
$list = glob('temp*.php');
var_dump($list);
Gives me this output :
给我这个输出:
array
0 => string 'temp-2.php' (length=10)
1 => string 'temp.php' (length=8)
While this one :
而这个:
$list = glob('te*-*');
var_dump($list);
Yes, with two *;-)
是的,有两个*;-)
Will give me :
会给我:
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)
回答by Gordon
As of PHP5.3, you can also use the GlobIteratorto search a directory with wildcards:
从 PHP5.3 开始,您还可以使用GlobIterator来搜索带有通配符的目录:
$it = iterator_to_array(
new GlobIterator('/some/path/*.pdf', GlobIterator::CURRENT_AS_PATHNAME) );
would return the full paths to all .pdf files in some/path in an array. The above performs about the same as glob(), but iterators provide a more powerful and extensible API.
将返回数组中 some/path 中所有 .pdf 文件的完整路径。上面的执行与 大致相同glob(),但迭代器提供了更强大和可扩展的 API。
回答by luciomonter
As long as file_exists returns a BOOLEAN I wrote this small function that accepts a search string with * to look for files... Example:
只要 file_exists 返回一个 BOOLEAN 我写了这个小函数,它接受一个带有 * 的搜索字符串来查找文件......示例:
searchForFile("temp*");
function searchForFile($fileToSearchFor){
$numberOfFiles = count(glob($fileToSearchFor));
if($numberOfFiles == 0){ return(FALSE); } else { return(TRUE);}
}
回答by Adam Gotterer
If you need a little more control and are on pre PHP 5.3 you could use a DirectoryIterator or RecursiveDirectoryIterator. Both have a lot of great function for added control and manipulation.
如果您需要更多控制并且使用 PHP 5.3 之前的版本,您可以使用 DirectoryIterator 或 RecursiveDirectoryIterator。两者都有很多很棒的功能来增加控制和操作。
PHP docs are at DirectoryIteratorand RecursiveDirectoryIterator

