php 查询指定目录下包含特定内容的文件

<?php

set_time_limit(0);

ini_set('memory_limit','512m');

$path = 'D:\wamp\www\php-5.4.12';

$info = '';

if(isset($_POST['submit'])){

$file_arr = array();

function scanFolder($dir,$info){

 $files = scandir($dir);

 foreach($files as $file){

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

  $file = $dir.DIRECTORY_SEPARATOR.$file;

  if(is_dir($file)){

  scanFolder($file,$info);

  }else if(is_file($file)){

  showRet($file,$info);

  }

 }

 }

  }

  function showRet($file,$info){

 global $file_arr;

 $arr = file($file);

 $line = 0;

 foreach($arr as $text){

 ++$line;

 if(strpos($text,$info) !== false){

 echo $file,':',$line,'--',$text,'<br/>';

 $file_arr[$file] = 1;

 }

 }

  }

  $path = trim($_POST['path']);

  $info = trim($_POST['info']);

  

  if(is_dir($path)){

      scanFolder($path,$info);

 if($file_arr){

 echo '搜到的文件<br/>';

 foreach($file_arr as $file=>$v){

 echo $file,'<br/>';

 }

 }

  }else{

 echo $path,'is not a folder';

 }

  

}

?>

<html>

<head>

</head>

<body>

<div class="searchBar" style="margin-top:50px;">

<form method="post" action="" >

<p>

<span>

搜索路径:<input type="text" value="<?php echo $path;?>" name="path" />

</span>

</p>

<p align="left" style="margin-left: 200px;"> 

<span>

搜索内容:<input type="text" value="<?php echo $info;?>" name="info" />

</span>

</p>

<div style="margin-top: 10px;"><input type="submit" name="submit" value="提交" />

</form>

</div>

</body>

</html>


转载于:https://my.oschina.net/u/196016/blog/226228

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值