請參考如下作法:
function getDirFilePathAry($directory, &$filePathAry)
{
$mydir = dir($directory);
while($file = $mydir->read())
{
if($file != "." && $file != ".." &&
strpos($file, '.php') !== false)
{
if((is_dir("$directory/$file")))
{
getDirFilePathAry("$directory/$file", $filePathAry);
}else{
$filePathAry[] = $directory/$file;
}
}
}
$mydir->close();
}
function findUrlFileAry($filePathAry, $url)
{
$fileAry = array();
foreach($filePathAry as $file)
{
$fileStrAry = file($file, FILE_IGNORE_NEW_LINES);
foreach($fileStrAry as $str)
{
if(strpos($str, $url) !== false))
{
$fileAry[] = $file;
break;
}
}
}
return $fileAry;
}
$filePathAry = array();
$directory = $_SERVER['DOCUMENT_ROOT']."/";
// 取得所有檔案
getDirFilePathAry($directory, $filePathAry);
$url = 'www.test.com';
// 取得有url的所有檔案
$fileAry = findUrlFileAry($filePathAry, $url);
echo '<pre>';
var_dump($fileAry);
echo '</pre>';