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
$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.
Use the PHP glob() function to get the list of the files by pattern and delete using the loop.
foreach (glob("messages.*.inc.php") as $filename) {
unlink($filename);
}