我有一个简单的脚本,在每个子文件夹内的每个文件中搜索给定的字符串。它一直很好用,直到我认为我的PHP被更新了(我不确定是否是因为这个原因)。这是代码:
<?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>
我遇到的错误是:注意:file_get_contents():在第29行中读取8192字节时errno=21是一个目录
有没有人有任何可能解决这个问题的想法?
如果您能帮我解决这个问题,我将非常感激。谢谢! :)
if(is_dir($path))
的条件下,你将$is_file
设置为 true,而实际上应该是 false。 - Chris Haas$path
之后,您正在检查$path
是否为目录。如果是,则将$is_file
设置为true。这是有意为之吗?因为$is_file
为true且$path
指向目录,所以在几行代码下面的file_get_contents()
可能会在目录上运行。 - Niko Nymanif (is_dir($path)) { do_directory_stuff } else if if (is_file($path)) { do_file_stuff}
。第二个if
是对其他文件类型(如设备或管道)的保护。 - Wiimm