0

I am trying to make a PHP application which searches through the files of your current directory and looks for a file in every subdirectory called email.txt, then it gets the contents of the file and compares the contents from email.txt with the given query and echoes all the matching directories with the given query. But it does not work and it looks like the problem is in the if-else part of the script at the end because it doesn't give any output.

<?php
// pulling query from link
$query = $_GET["q"];
echo($query);
echo("<br>");

// listing all files in doc directory
$files = scandir(".");

// searching trough array for unwanted files
$downloader = array_search("downloader.php", $files);
$viewer = array_search("viewer.php", $files);
$search = array_search("search.php", $files);
$editor = array_search("editor.php", $files);
$index = array_search("index.php", $files);
$error_log = array_search("error_log", $files);
$images = array_search("images", $files);
$parsedown = array_search("Parsedown.php", $files);

// deleting unwanted files from array
unset($files[$downloader]);
unset($files[$viewer]);
unset($files[$search]);
unset($files[$editor]);
unset($files[$index]);
unset($files[$error_log]);
unset($files[$images]);
unset($files[$parsedown]);

// counting folders
$folderamount = count($files);

// defining loop variables
$loopnum = 0;

// loop
while ($loopnum <= $folderamount + 10) {
    $loopnum = $loopnum + 1;

    // gets the emails from every folder
    $dirname = $files[$loopnum];
    $email = file_get_contents("$dirname/email.txt");
    //checks if the email matches
    if ($stremail == $query) {
        echo($dirname);
    }
}

//print_r($files);
//echo("<br><br>");
?>

Can someone explain / fix this for me? I literally have no clue what it is and I debugged soo much already. It would be heavily gracious and appreciated.

Kind regards, Bluppie05

Bluppie05
  • 113
  • 8
  • Start debugging, eg. output both the email content and the query to screen.. you'll probably want to check, if the search-term is _contained_ in the email (not equal!) – Honk der Hase Mar 06 '20 at 23:21

1 Answers1

0

There's a few problems with this code that would be preventing you from getting the correct output.

The main reason you don't get any output from the if test is the condition is (presumably) using the wrong variable name.

  // variable with the file data is called $email
  $email = file_get_contents("$dirname/email.txt");

  // test is checking $stremail which is never given a value
  if ($stremail == $query) {
    echo($dirname);
  }

There is also an issue with your scandir() and unset() combination. As you've discovered scandir() basically gives you everything that a dir or ls would on the command line. Using unset() to remove specific files is problematic because you have to maintain a hardcoded list of files. However, unset() also leaves holes in your array, the count changes but the original indices do not. This may be why you are using $folderamount + 10 in your loop. Take a look at this Stack Overflow question for more discussion of the problem.

Rebase array keys after unsetting elements

I recommend you read the PHP manual page on the glob() function as it will greatly simplify getting the contents of a directory. In particular take a look at the GLOB_ONLYDIR flag.

https://www.php.net/manual/en/function.glob.php

Lastly, don't increment your loop counter at the beginning of the loop when using the counter to read elements from an array. Take a look at the PHP manual page for foreach loops for a neater way to iterate over an array.

https://www.php.net/manual/en/control-structures.foreach.php

ArchCodeMonkey
  • 2,264
  • 1
  • 10
  • 9
  • I know that $stremail is never given a value, that was an error from a different test but if I use $email it still doesn't want to work – Bluppie05 Mar 07 '20 at 11:53
  • Btw thanks for answering, it didn't work but I have rewritten my whole script in another way and now it is fully working. – Bluppie05 Mar 07 '20 at 17:16