Javascript 从PHP中动态上载的目录中的.txt文件中查找关键字

Javascript 从PHP中动态上载的目录中的.txt文件中查找关键字,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我正在做一个项目,其中包括一个.tar文件将被上传到用户界面,它将被脚本解析和解压现在这个捆绑包将包含许多文件夹,我需要单独访问每个文件夹,并搜索其中的.txt文件中的关键字,以便它在用户界面上显示包含关键字的整行内容 我使用下面的脚本在文件夹中的不同.txt文件中搜索关键字,但当有多个文件夹时,逻辑无法工作,它需要访问单独的文件夹并显示不同文件夹的.txt文件中的关键字 在检查了这里和那里的代码后,得到了这个。这将逐个读取主/子/文件夹中的文件 $searchfor = $_GET['key

我正在做一个项目,其中包括一个.tar文件将被上传到用户界面,它将被脚本解析和解压现在这个捆绑包将包含许多文件夹,我需要单独访问每个文件夹,并搜索其中的.txt文件中的关键字,以便它在用户界面上显示包含关键字的整行内容

我使用下面的脚本在文件夹中的不同.txt文件中搜索关键字,但当有多个文件夹时,逻辑无法工作,它需要访问单独的文件夹并显示不同文件夹的.txt文件中的关键字


在检查了这里和那里的代码后,得到了这个。这将逐个读取主/子/文件夹中的文件

$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;
$searchfor=$\u GET['keyword'];
$path='主文件夹';
$dirs=array();
//目录句柄
$dir=dir($path);
//这将包含主文件夹中的所有子目录
while(false!==($entry=$dir->read()){
如果($entry!='.&&$entry!='..')){
if(is_dir($path.'/'.$entry)){
$dirs[]=$path.'/'.$entry.'/';
}
}
}
//回声“;印刷费($dirs);出口
//此文件夹用于读取主文件夹中的每个子文件夹
foreach($dirs作为$directory):
//$linesToReturn=4;//将每个文本文件中的行数设置为4(用于扩展?)
//查找目录中所有.xyz文件
foreach(glob($directory.*.INFO)作为$filename)
{
//echo$filename;
//打开文件
如果($handle=@fopen($filename,“r”))
{
$x=0;//启动行计数器
//循环每行,直到结束或达到返回限值
而(!feof($handle))
{
$contents=fgets($handle);//读取该行
//然后将搜索与内容匹配
$x++;//增加计数器
}
}
}
endforeach;

请检查并澄清。

在检查了这里和那里的代码后,得到了这个。这将逐个读取主/子/文件夹中的文件

$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;
$searchfor=$\u GET['keyword'];
$path='主文件夹';
$dirs=array();
//目录句柄
$dir=dir($path);
//这将包含主文件夹中的所有子目录
while(false!==($entry=$dir->read()){
如果($entry!='.&&$entry!='..')){
if(is_dir($path.'/'.$entry)){
$dirs[]=$path.'/'.$entry.'/';
}
}
}
//回声“;印刷费($dirs);出口
//此文件夹用于读取主文件夹中的每个子文件夹
foreach($dirs作为$directory):
//$linesToReturn=4;//将每个文本文件中的行数设置为4(用于扩展?)
//查找目录中所有.xyz文件
foreach(glob($directory.*.INFO)作为$filename)
{
//echo$filename;
//打开文件
如果($handle=@fopen($filename,“r”))
{
$x=0;//启动行计数器
//循环每行,直到结束或达到返回限值
而(!feof($handle))
{
$contents=fgets($handle);//读取该行
//然后将搜索与内容匹配
$x++;//增加计数器
}
}
}
endforeach;

请检查并澄清。

文件夹结构如何?比如主文件夹/sub-folder1/1.txt,2.txt。sub-folder2/1.txt,2.txt?@Plum-感谢您的回复,文件夹结构就像一个主文件夹,有子文件夹,子文件夹的数量不固定,可以从1到16不等。在每个文件夹中可以有1到24个.txt或.INFO文件,我需要搜索,这些文件的数量也是固定的,但在1到24之间。因此,您可以说它类似于主文件夹/子文件夹/*.info或*.txt文件。文件夹结构如何?比如主文件夹/sub-folder1/1.txt,2.txt。sub-folder2/1.txt,2.txt?@Plum-感谢您的回复,文件夹结构就像一个主文件夹,有子文件夹,子文件夹的数量不固定,可以从1到16不等。在每个文件夹中可以有1到24个.txt或.INFO文件,我需要搜索,这些文件的数量也是固定的,但在1到24之间。所以你可以说它就像主文件夹/子文件夹/*.info或*.txt文件一样,我在这里有一些疑问,代码在哪里访问子文件夹来搜索文件?是否需要在$path='master folder'条目中输入完整路径。另外,注释中的match your contents with search将包含我的php脚本以匹配文件中的关键字?您是否看到顶部有一个while循环,带有注释语句//echo“”;印刷费($dirs);出口Plum,我有一些疑问,代码在哪里访问子文件夹以搜索文件?是否需要在$path='master folder'条目中输入完整路径。另外,注释中的match your contents with search将包含我的php脚本以匹配文件中的关键字?您是否看到顶部有一个while循环,带有注释语句//echo“”;印刷费($dirs);出口