I am trying to write some code to look through all of the files in my server and return files that contain a certain string. The problem is that I only know the comment in the files I'm looking for is the key and I feel like this may be messing this up.
I have a function that recursively searches for files in all directories which works fine, but the reading of the file and searching of the string is not working properly.
<?php
$mal = "//###=CACHE START=###";
function getDirContents($dir) {
$files = scandir($dir);
foreach($files as $file) {
if($file == "." || $file == "..") continue;
if(!is_file($dir . $file)){
echo "Folder: " . $dir . $file . "<br />";
getDirContents($dir.$file."/");
} else {
echo "File: " . $dir . $file . "<br />";
$content = file_get_contents($dir . $file);
if (strpos($content, $mal) !== false) {
echo "FOUND" . $dir.$file . "<br>";
}
}
}
}
$dir = "./";
getDirContents($dir);
?>
For some reason, this is returning .png and .jpg files as "FOUND" and I'm not sure why. I have many files that have the $mal string in them, but it's a comment and I'm not sure if that matters. Either way, it is not working properly and not finding the files that I'm looking for.