In PHP, how to search for a file with a similar name? There are three files: file.csv
, file.csv.1
and olderfile.csv
.
How to find the files file.csv
and file.csv.1
?
Depend on how you define "similar." And try use regular expression.
$filename = basename("/some/long/path/file.csv");
// Or other vars pointing to your actual file.
$pattern = '/(^file\.csv(?:\.\d|))/';
if(preg_match($pattern, $filename, $matches)){
print_r($matches);
} else {
echo "Not found.";
}
You can use the glob() function for this. This should work with the files you mentioned:
foreach (glob("file.csv*") as $filename) {
echo "$filename
";
}