如何查找名称末尾包含动态子字符串的文件
How to find a file which contains dynamic substring at the end of its name
我想创建一个 php 脚本来查找名称为 favicon_323123.ico
的所有文件。当然,字符串 _323123
不是静态的。
这是我当前的代码:
<?php
$filepath = recursiveScan('/public_html/wp-admin/');
$text = 'favicon' .*. 'ico'
function recursiveScan($dir) {
$tree = glob(rtrim($dir, '/') . '/*');
if (is_array($tree)) {
foreach($tree as $file) {
if (is_dir($file)) {
//echo $file . '<br/>';
recursiveScan($file);
} elseif (is_file($file)) {
if (strpos($file, $text) !== false) {
echo $file . '<br/>';
//unlink($file);
}
}
}
}
}
?>
来自
if (strpos($file, $text) !== false) {
到
if (preg_match( "/favicon_[0-9]+\.ico$/", $file )) {
preg_match 调用,检查文件是否是您要查找的文件。
我想创建一个 php 脚本来查找名称为 favicon_323123.ico
的所有文件。当然,字符串 _323123
不是静态的。
这是我当前的代码:
<?php
$filepath = recursiveScan('/public_html/wp-admin/');
$text = 'favicon' .*. 'ico'
function recursiveScan($dir) {
$tree = glob(rtrim($dir, '/') . '/*');
if (is_array($tree)) {
foreach($tree as $file) {
if (is_dir($file)) {
//echo $file . '<br/>';
recursiveScan($file);
} elseif (is_file($file)) {
if (strpos($file, $text) !== false) {
echo $file . '<br/>';
//unlink($file);
}
}
}
}
}
?>
来自
if (strpos($file, $text) !== false) {
到
if (preg_match( "/favicon_[0-9]+\.ico$/", $file )) {
preg_match 调用,检查文件是否是您要查找的文件。