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! :)
if(is_dir($path))
you then set$is_file
to true, when it should be false. – Horan$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 thefile_get_contents()
is probably being run on a directory. – Akelaif (is_dir($path)) { do_directory_stuff } else if if (is_file($path)) { do_file_stuff}
. The secondif
is a guard for other file types like devices or pipes. – Fishbein