How to search file with pattern [closed]

2019-09-03 00:58发布

I have a folder with many file , and i need to perform a deletion with certain file and those file have a pattern like

messages.bm.inc.php 
messages.cn.inc.php 
messages.en.inc.php

Those file are dynamically created , but the pattern is there

Before this i normally delete my file with below code , and repeat it

$filename="messages.en.inc.php";

if (file_exists($filename)) {
    unlink($filename);
}

Now i having a more dynamic situation , i need search through those file with the patern and delete it , please suggest a way to do , thanks

标签: php file search
3条回答
疯言疯语
2楼-- · 2019-09-03 01:38
$files = glob("path_to_your_files/messages.*.inc.php ");
array_map('unlink', $files);

By glob you will get all your files from folder by specified pattern, array_map will implement unlink function for array of matched files.

查看更多
Luminary・发光体
3楼-- · 2019-09-03 01:41

Use the PHP glob() function to get the list of the files by pattern and delete using the loop.

查看更多
贪生不怕死
4楼-- · 2019-09-03 01:49
foreach (glob("messages.*.inc.php") as $filename) {
    unlink($filename);
}
查看更多
登录 后发表回答