Edit
There is nothing wrong with my code so feel free to use it as it is, if ever you need to search your file system from within PHP then echo the results.
I created a class to search through files using glob
. It worked perfectly but now that I have migrated from Apache to Nginx, it always returns 0 results.
Here is my code:
public static function search($path, $find, $caseSensitive = false)
{
if ($path[strlen($path) - 1] !== '/')
$path .= '/';
$path = '../'.$path;
$pathLen = strlen($path);
$path .= '*';
if ($caseSensitive)
$files = self::globRecursive($path.$find);
else
{
$findLen = strlen($find);
for ($i = 0; $i < $findLen; $i++)
$find1 .= '['.strtolower($find[$i]).strtoupper($find[$i]).']';
$files = self::globRecursive($path.$find1);
}
$message = '';
$count = count($files);
if ($count === 0)
return '"'.$find.'" not found.';
foreach ($files as $file)
$message .= substr($file, $pathLen).'<br />';
return '"'.$find.'" found in '.$count.' files:<br />'.$message;
}
private static function globRecursive($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
$files = array_merge($files, self::globRecursive($dir.'/'.basename($pattern), $flags));
return $files;
}