0
我使用此代码来搜索服务器上的PHP文件中的特定代码。问题是,它通常需要一些时间并发出致命错误(超出内存限制)。有没有办法让代码更“优雅”?PHP文件搜索内存限制
<?php
ini_set('display_errors', 1);
ini_set('memory_limit', '128M');
error_reporting(E_ALL);
define("SLASH", stristr($_SERVER['SERVER_SOFTWARE'], "win") ? "\\" : "/");
if (isset($_POST['path']))
$path = ($_POST['path']) ? $_POST['path'] : dirname(__FILE__) ;
else
$path = dirname(__FILE__) ;
if (isset($_POST['q']))
$q = $_POST['q'];
function php_grep($q, $path){
$temp_array = array();
$ret = '';
$fp = opendir($path);
while($f = readdir($fp)){
if(preg_match("#^\.+$#", $f)) continue; // ignore symbolic links
$file_full_path = $path.SLASH.$f;
if(is_dir($file_full_path)) {
$ret .= php_grep($q, $file_full_path);
} else if(stristr(file_get_contents($file_full_path), $q)) {
$ret .= "$file_full_path\n";
}
}
foreach($temp_array as $t)
$ret .= php_grep1($q, $t);
return $ret;
}
if (isset($_POST['q'])){
$results = php_grep($q, $path);
}
?>
<form method=post>
<input name=path size=100 value="<?php echo $path ?>" /> Path
<input name=q size=100 value="<?php if (isset ($q)) echo $q; else echo 'mysql_connect';?>" /> Query
<input type=submit>
</form>
<?php if (isset($results)) echo $results."<br/>"; ?>
SSH被禁用,所以我不能使用它。 叹息 grep本来是一个更简单的解决方案。
谢谢你们。
下一次做:粘贴你的代码,选择你的代码,按'{}' – 2011-05-09 04:29:04