8

I have a simple script, that searches inside every file of every subfolder for a given string. It worked just fine, until I believe my PHP got updated (I'm not pretty sure if it was because of that). This is the code:

<?php 
$limit = ini_get('memory_limit');
ini_set('memory_limit', -1);
ini_set('max_execution_time', 300);

function get_directory_content($directory){
    global $search, $results; 

    $files = scandir($directory);

    foreach ($files as $file) {
        if ($file == "." || $file == "..") {
            continue;
        }

        $is_file = false;
        $path = realpath($directory . DIRECTORY_SEPARATOR . $file);

        if (is_dir($path)) {
            get_directory_content($path);
            $is_file = true;
        }
        else{
            $is_file = true;
        }

        if ($is_file) {
            $content = file_get_contents($path);
        }


        if (stripos($content, $search) !== false) {
            $obj = new stdClass();
            $obj->dir = ($directory . DIRECTORY_SEPARATOR . "<b>".$file."</b>");
            $obj->file_name = $file;
            array_push($results, $obj);
        }
    }
}

if (isset($_GET["submit"])) {
    $search = $_GET["search"];
    $results = array();

    get_directory_content(dirname(__FILE__));
}

 ?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>Main</h1>
        <form method="GET">
            <p>
                <input name="search" autocomplete="off" placeholder="Enter query here">
            </p>
            <input type="submit" name="submit">
        </form>


        <?php foreach ($results as $result):  ?>
            <h1><?php echo $result->file_name; ?></h1>
            <p><?php echo $result->dir; ?></p>
        <?php endforeach; ?>
</body>
</html>

And the error that I'm getting is: Notice: file_get_contents(): read of 8192 bytes failed with errno=21 Is a directory in /Users/nikolaynikolaev/Server/original_search.php on line 29

Does anyone have any idea of a potential fix for that issue?

I would be more than grateful if you help me with a fix. Thanks in advance! :)

Nikolai Nikolaew
  • 101
  • 1
  • 1
  • 4
  • 1
    This appears to be an attempt at a recursive function but there's several wrong things with it. Your immediate problem is that in the condition where you check `if(is_dir($path))` you then set `$is_file` to true, when it should be false. – Chris Haas Jun 23 '20 at 21:10
  • 1
    After setting `$path` you're checking if `$path` is a directory. If it is, you're setting `$is_file=true`. Is this intentional? Because `$is_file` is true and `$path` points to directory, a few lines down the `file_get_contents()` is probably being run on a directory. – Niko Nyman Jun 23 '20 at 21:17
  • What @ChrisHaas said. :P – Niko Nyman Jun 23 '20 at 21:17
  • 1
    Wow! Thank you guys! @NikoNyman and ChrisHaas ! I am actually studying right now, and just got the hang of C#, but PHP is still not one of my powers. It was a very silly mistake, but thanks to you I was able to fix it. Thanks a lot guys! :) – Nikolai Nikolaew Jun 24 '20 at 10:07
  • There is another logical error: If a directory is found, the content of the previous file is analyzed. So why not: `if (is_dir($path)) { do_directory_stuff } else if if (is_file($path)) { do_file_stuff}`. The second `if` is a guard for other file types like devices or pipes. – Wiimm Jun 09 '22 at 17:19

1 Answers1

8

The notice message explains it clearly with "Is a directory".

Notice: file_get_contents(): read of [...] bytes failed with errno=21 Is a directory in [...]

To fix this error just check the path

foreach ($paths as $path) {
    if (!is_file($path))
        continue;
    
    // this path points to a file
}
Phil
  • 422
  • 1
  • 5
  • 20