从 PHP 动态上传的目录中存在的.txt文件中查找关键字
Finding a Keyword from a .txt file present in directories uploaded dynamically in PHP
我正在做一个项目,其中包括一个.tar文件将被上传到UI,它将通过脚本解析并解压缩 现在这个捆绑包将包含许多文件夹,我需要单独访问每个文件夹并在其中搜索.txt文件以获取关键字,以便它将包含关键字的整行显示在UI上。
我有以下脚本,我正在使用该脚本在文件夹内的不同.txt文件中搜索关键字,但是当有多个文件夹并且需要访问单独的文件夹并显示来自不同文件夹的文件.txt关键字时,逻辑无法正常工作
<?php
$searchfor = $_GET['keyword'];
$file = array('alert_tool.INFO','alert_manager.INFO');
foreach($file as $name)
{
echo "'n"; echo"'n";
$contents = file_get_contents($name);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*'$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:<br />";
echo implode("<br /> $name", $matches[0]);
}
else{
for ($i = 0; $i < 2; $i++)
{
echo nl2br ("'n'n'n'n'n No matches found in $file[$i]");
}
}
@fclose ($file);
}
?>
任何帮助都非常感谢。谢谢。
在通过 SO 这里的代码后,得到了这个。这将读取主/子/文件夹中的文件
$searchfor = $_GET['keyword'];
$path = 'master-folder';
$dirs = array();
// directory handle
$dir = dir($path);
//This will have all the sub directories within the master-folder
while (false !== ($entry = $dir->read())) {
if ($entry != '.' && $entry != '..') {
if (is_dir($path . '/' .$entry)) {
$dirs[] = $path . '/' .$entry."/";
}
}
}
//echo "<pre>"; print_r($dirs); exit;
// This foreach is to read each sub folders inside the master-folder
foreach($dirs as $directory):
//$linesToReturn = 4; // Set to four for the number of lines in each text file ( for expansion? )
// Find all files in the directory which are .xyz files
foreach( glob( $directory . "*.INFO" ) as $filename )
{
//echo $filename;
// Open the file
if( $handle = @fopen( $filename, "r") )
{
$x = 0; // Start the line counter
// Cycle each line until end or reach the lines to return limit
while(! feof( $handle ) )
{
$contents = fgets($handle); // Read the line
// then match your search with the contents
$x++; // Increase the counter
}
}
}
endforeach;
请检查并澄清。
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 通过php页面中的js强制下载txt
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 用于检查数组中是否存在元素的javascript自定义方法
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 正在将事件处理程序添加到不存在的类
- 是否存在React Native“;WEB代码安全防护”;
- 一个密码测试程序,如果存在空格,它会提醒用户
- 验证会话中是否存在对象's数组
- javascript如果图像不存在don't加载它
- 如何查找值是否存在于二叉树中
- IE9的HTML5 Canvas getImageData()函数存在问题
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- CORS-服务器端cookie没有保存在chrome浏览器上
- 如果代码没有'txt文件中不存在
- 从 PHP 动态上传的目录中存在的.txt文件中查找关键字
- 如何创建单击计数器,即使刷新也将数据保存在txt文件中