How can i check if file exists based on a partial

2020-03-26 06:38发布

Im trying to check if a file exists in my folder but i only have a partial filename to check it against, Is there a way to check it?

For example i have the following:

God_of_War

The filename is actually called:

God_of_War_PSP(USA).rar

Is there some kind of LIKE for file_exists function ? or can i use str_pos somehow ?

Thanks

标签: php file
2条回答
劫难
2楼-- · 2020-03-26 06:47

You could use something similar to the following:

$fileSearch = "God_of_War";
$files = glob("/path/*" . $fileSearch . "*");
if(count($files) > 0) echo "File Exists!";
查看更多
Luminary・发光体
3楼-- · 2020-03-26 07:05

You could also try something like:

$search_string = 'God_of_War';
$dir = new RecursiveDirectoryIterator('path/to/folder/');
$iterator = new RecursiveIteratorIterator($dir);
$res = new RegexIterator($iterator, '/^'.$search_string.'/', RecursiveRegexIterator::GET_MATCH);
var_dump($res);
查看更多
登录 后发表回答