-1

i have PHP code that read multiple TXT files and allow the user to make a search on a specified string, and after the system read all text files and based on the user input the system will display the files name that contain the user request.

the problem is that when i run the code the webpage display :

Warning: fopen() expects parameter 1 to be a valid path, array given in C:\xampp\htdocs\readfiletest\index.php on line 11

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\readfiletest\index.php on line 64

where the error in my code and how to fix it ?

code:

<?php

//path to directory
$directory = $_SERVER['DOCUMENT_ROOT']."/readfiletest/";
$txts= glob($directory. "*.txt") or DIE("Unable to open $directory");

$myFileLink = fopen($txts, 'r');

$line = 1; 

if(isset($_POST["search"]))
{
    $search =$_POST['name'];

 while(!feof($myFileLink)) 
 { 
     $myFileContents = fgets($myFileLink);
     if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
     {

        foreach($matches[1] as $match)
        {
           echo "Found $match on Line $line";
        }

     }

     ++$line;

 }

}

    fclose($myFileLink);

    ?>

<html>
    <head>
    </head>
    <meta http-equiv="Content-Language" content="ar-sa">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <body>
     <form action="index.php" method="post">
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p><input type ="Submit" name ="search" value= "Search" /></p>
    </form>
    </body>
</html>
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Rany Fahed
  • 39
  • 3
  • 11
  • Have you echo the $txts variable before fopen. what path are you getting? – Deep Kakkar Dec 28 '17 at 07:54
  • @DeepKakkar it display this note: **Notice: Array to string conversion in C:\xampp\htdocs\readfiletest\index.php on line 7 Array** – Rany Fahed Dec 28 '17 at 07:57
  • 2
    The error messages tell you exactly what the problem is. Your $txts is an array and you can't open an array. You need to loop the array and open each array value one by one – Andreas Dec 28 '17 at 07:58

2 Answers2

1

If you only want to fix the problem you need something like this:

$directory = $_SERVER['DOCUMENT_ROOT']."/readfiletest/";
$txts= glob($directory. "*.txt") or DIE("Unable to open $directory");

Foreach ($txts as $txt){
   $myFileLink = fopen($txt, 'r');
   // .... And so on..

This will open one file at the time and search them.


If you want to improve the code a bit my advice is to first open the file with file_get_contents and do the search on the full text.
If there is a match you can try to find the line number.

Something like:

$directory = $_SERVER['DOCUMENT_ROOT']."/readfiletest/";
$txts= glob($directory. "*.txt") or DIE("Unable to open $directory");

Foreach ($txts as $txt){
    $myFileContents = file_get_contents($txt);
   If(preg_match('/('.preg_quote($search,'/').')/i', $myFileContents, $matches)){
     // Here we know there is a match in the file, if there is no match there is no need to search each line
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • I tried your answer but when i tried to search for a string the system give this message : **Notice: Array to string conversion in C:\xampp\htdocs\readfiletest\index.php on line 50 Found انتقادات in these files Array Notice: Array to string conversion in C:\xampp\htdocs\readfiletest\index.php on line 50 Found انتقادات in these files Array** this string exist in 2 files – Rany Fahed Dec 28 '17 at 08:26
  • Line 50? The code you supply us with has 49 lines. How are we supposed to help you? – Andreas Dec 28 '17 at 08:35
  • my mistake sorry i will fix it i will edit my question because i found the solution but still i need to fix a little issue – Rany Fahed Dec 28 '17 at 08:37
  • it shows line 50 because i have a comment lines but i did not display it so now i am runing the code as its on this post – Rany Fahed Dec 28 '17 at 08:41
  • i notice that the system combine all file in one file and continue the counting of line numbers .... how can i separate each file to make the system start counting from the first line in each file – Rany Fahed Dec 28 '17 at 08:46
  • @Rany You can't do like this. You have completely changed the question. I flagged this question for moderators with the text *Question has changed. Original question is completely gone from the question. I think it should be deleted* – Andreas Dec 28 '17 at 08:49
  • Reset the counter, seriously. It's not rocket science – Andreas Dec 28 '17 at 08:51
  • thank you and sorry for changing the question but i did not know that it can't be done like this. and for the reset counter i fix it – Rany Fahed Dec 28 '17 at 08:55
0

I recommend using "Finder" Componnent from symfony

http://symfony.com/doc/current/components/finder.html

https://github.com/symfony/finder

In finder it should be something like that

$finder=new Finder();
$finder->in($directory)->files()->name('*.txt')->contains($search);

// and it's all -  simple  - isn't  it ? 
//to see all result  you do 

foreach ($finder as $file) {
    $contents = $file->getContents();

    // ...
}

You can easilly use it without whole symfony (installing via composer) .

I know that may look a little complicated to you, but trust me - this is a best php solution for searching in files , and it's definitely worth to learn

Michał G
  • 2,234
  • 19
  • 27