首页 > 解决方案 > 从我的 public_html 文件夹中可用的所有文件夹中删除具有特定名称的文件

问题描述

我的服务器根目录及其文件夹中散布着一个恶意文件。是否有任何方法/ php 脚本可以使用它一次从所有文件夹中删除所有 em。

标签: phpfile-handling

解决方案


function deleteFileFromDir($dir, $filename){
    $ffs = scandir($dir);

    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);

    foreach($ffs as $ff){
        if(is_dir($dir.'/'.$ff)){
            deleteFileFromDir($dir.'/'.$ff, $filename);
        } else {
            if( $ff == $filename ){
                echo "found ".$dir.'/'.$ff.'<br>';
                unlink($dir.'/'.$ff);
            }
        }
    }
}

$search_dir_path = '.'; // The same folder as the file location
$search_file = 'some_file_to_delete.txt';
deleteFileFromDir($search_dir_path, $search_file);

只需根据需要更改$search_dir_path$search_file变量。


推荐阅读