How to search for a file with a picture

I have a folder with many files and I need to perform a delete with a specific file and this file has a pattern like

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

      

These files are dynamically generated, but the template exists

Before that, I usually delete my file using the code below and repeat it

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

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

      

Now that I have a more dynamic situation, I need to search through this file using patern and delete it, please suggest a way to do it, thanks

0


source to share


3 answers


$files = glob("path_to_your_files/messages.*.inc.php ");
array_map('unlink', $files);

      



In glob

you will get all your files from the folder according to the specified template, array_map

will implement the function unlink

for an array of matching files.

+6


source


foreach (glob("messages.*.inc.php") as $filename) {
    unlink($filename);
}

      



+4


source


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

0


source







All Articles