php - 错误 file_get_contents(): 读取 8192 字节失败,errno=21
问题描述
我有一个简单的脚本,它在每个子文件夹的每个文件中搜索给定的字符串。它工作得很好,直到我相信我的 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(): read of 8192 bytes failed with errno=21 Is a directory in /Users/nikolaynikolaev/Server/original_search.php on line 29
有没有人知道该问题的潜在解决方案?
如果您能帮我解决问题,我将不胜感激。提前致谢!:)
解决方案
通知消息用“是一个目录”清楚地解释了它。
注意:file_get_contents(): read of [...] bytes failed with errno=21 is a directory in [...]
要修复此错误,只需检查路径
foreach ($paths as $path) {
if (!is_file($path))
continue;
// this path points to a file
}
推荐阅读
- linux - vscode服务器启动SSH失败
- reactjs - vscode reformatting react js
- javascript - Wordpress 在“自定义帖子类型”的内容部分中的所有图像周围添加一个 div
- reactjs - Anyway to scroll through Carbon Design System content switchers?
- list - Rstudio 中是否有一个函数可以在列中以及从具有该名称的所有信息的列表中查找具有相同名称的数据?
- excel - 是否有将屏幕剪辑分配给命令按钮的 vba 代码?
- haskell - 如何将一个函数中收到的列表传递给另一个函数 - 在 Haskell 中?
- javascript - 当我在赛普拉斯自动化中发出 POST 请求时,出现身份验证错误“无法在请求中找到凭据”
- python - NumPy 1.21.2 可能还不支持 Python 3.10
- bash - 在 Bash 中调用算术展开运算符时如何使用乘法和除法?