I am sitting in a quiet dormitory now, and the winter vacation seems to have left me... What I posted today is the last study I did during the winter vacation, and I have been watching One Piece since then.
I used to write a C language program that traverses directories + copies files. It was very long and complicated. Now I can use PHP to traverse directories, and the code is much shorter. The purpose of this program is to traverse the directory, find all files with the specified file name, and delete the specified string.
$tmpfiledir = $_SERVER["DOCUMENT_ROOT"].'tmp.txt';
function del($getstr)
{
$isbak = true; //Whether to back up the original file, true means backup, false does not back up
global $tmpfiledir;
$fr = fopen($tmpfiledir,"r") or die('Failed to open temporary file');
while($row = fgets($fr))
{
If(empty($row)) break;
$row = trim($row);
$opp = fopen($row,"r") or die("Failed to open $row");
$str = fread($opp,filesize($row)) or die("Cannot read $row");
$str = str_replace($getstr,"",$str);
fclose($opp);
if($isbak){
copy($row,$row.'.bak') or die("Backup file failed");
}
$ref = fopen($row,"w") or die("Failed to reopen file");
fwrite($ref,$str) or die("Failed to rewrite file");
}
}
function traverse($path) {
Global $name,$tmpfiledir;
$current_dir = opendir($path); //opendir() returns a directory handle, and returns false
on failure
if($current_dir == false)
Return false;
while(($file = readdir($current_dir)) !== false) { //readdir() returns an entry in the open directory handle
$sub_dir = $path . DIRECTORY_SEPARATOR . $file; //Build subdirectory path
If($file == '.' || $file == '..') {
Continue;
} else if(is_dir($sub_dir)) { //If it is a directory, perform recursion
Traverse($sub_dir);
} else { //If it is a file, compare again
$fileinfo = pathinfo($sub_dir);
If($fileinfo['basename'] == $name)
{
$fopen = fopen($tmpfiledir,"a");
fwrite($fopen,$sub_dir."rn");
fclose($fopen);
}
}
}
Return true;
}
if(isset($_POST["name"]) && isset($_POST["dir"]) && isset($_POST["str"]))
{
$name = $_POST["name"];
Traverse($_POST["dir"]) or die("Failed to create temporary file, please check whether the website root directory is writable");
del($_POST["str"]);
echo "success";
unlink($tmpfiledir);
}
else
{
echo "
Enter relevant information
";You can see that I wrote two functions. The function traverse writes the found specified file paths in a temporary file, and the function del deletes the specified strings in these files. In fact, I also think it is useless. I can just delete it during traversal without generating any temporary files at all.
Actually, I wrote the traversal in C language at the beginning. Because C language is not easy to operate files, I used PHP to write the deleted part. So I only wrote a del function at the beginning, and later I simply used it to traverse files. I wrote it in PHP (seems much simpler than C), so I wrote another function traverse. You can just look at the traversal part. You can also compare it with the traversal code I wrote in C before (I sent the source code) to see what the difference is.
However, this version does not support wildcards, so the file name must be specified. Its function (also the reason why I wrote this) is that it can delete the one-sentence Trojans we have hung on the server in batches.