爬PHP网站文件,文件爬虫PHP

想到两个直接的解决方案。

1)将grep与exec命令一起使用(仅当服务器支持时):

$query = $_GET['string'];

$found = array();

exec("grep -Ril '" . escapeshellarg($query) . "' " . $_SERVER['DOCUMENT_ROOT'], $found);完成后,包含查询的每个文件路径都将放在$found中。您可以遍历此数组并根据需要处理/显示它。

2)递归遍历文件夹并打开每个文件,搜索字符串,如果找到则保存:

function search($file, $query, &$found) {

if (is_file($file)) {

$contents = file_get_contents($file);

if (strpos($contents, $query) !== false) {

// file contains the query string

$found[] = $file;

}

} else {

// file is a directory

$base_dir = $file;

$dh = opendir($base_dir);

while (($file = readdir($dh))) {

if (($file != '.') && ($file != '..')) {

// call search() on the found file/directory

search($base_dir . '/' . $file, $query, $found);

}

}

closedir($dh);

}

}

$query = $_GET['string'];

$found = array();

search($_SERVER['DOCUMENT_ROOT'], $query, $found);这应该(未经测试)以递归方式搜索每个子文件夹/文件以查找所请求的字符串。如果找到,它将在变量$found中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值