Error file_get_contents(): read of 8192 bytes failed with errno=21

Error file_get_contents(): read of 8192 bytes failed with errno=21

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Error file_get_contents(): read of 8192 bytes failed with errno=21

When running a recursive file search script that suddenly stops working after a PHP update, the error message file_get_contents(): read of 8192 bytes failed with errno=21 is often the first thing you see. This specific error indicates that file_get_contents attempted to read from a path it believed was a file, but encountered a directory instead. While the code logic might seem sound at first glance, this issue stems from how PHP handles symbolic links and how directory traversal interacts with system constraints.

The root cause usually lies in the logic used to distinguish between files and directories during the recursive walk. In the provided script, the author uses realpath() to resolve the full path before checking if it is a file:

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

if (is_dir($path)) {
    // Recurse
} else {
    // Treat as file
}

The intention here is clear: resolve the path, check if it's a directory, and only read files. However, realpath() has a specific behavior regarding symbolic links. If a directory contains a symlink pointing to another directory (a "dirlink"), is_dir() returns true before following the link. But if that symlink points to a file, or more critically in some server configurations, if the resolution fails or behaves unexpectedly due to permissions or broken links, the logic can break down.

More commonly, this specific error occurs when scandir() includes directories that appear in the list but are actually symlinks to directories elsewhere on the filesystem. When the script checks $is_dir via is_dir($path), it correctly identifies a directory structure. However, if there is a race condition or a specific filesystem layout where a "file" entry in the directory listing is actually a broken symlink pointing to a directory, realpath() might fail silently or return the link itself.

Wait, looking closer at the provided code snippet and the error context: the error explicitly states errno=21, which stands for EISDIR (Is a Directory). This means PHP successfully resolved the path, determined it was not a file (or failed the check), but then tried to read it as one.

The most likely culprit in this specific implementation is the handling of symlinks. If $file is a symlink pointing to a directory, is_dir($path) returns true, so it recurses correctly. However, if $file is a regular file that happens to be empty or very large, and there are permission issues, or if the realpath() call resolves a symlink to a directory (e.g., /var/www/html/link -> /data/logs), the logic holds.

But consider this scenario: What if scandir() returns an entry that is a symlink to a directory? The code checks is_dir($path). If it's a link to a directory, is_dir is true, and it recurses. That seems correct. So why the error?

The error happens on line 29: $content = file_get_contents($path);. This line is only reached if is_file($path) is implicitly assumed or if the if ($is_file) block logic is flawed. In the provided code, $is_file is initialized to false. Inside the loop, it sets $is_file = true in both the directory branch (after recursion) and the else branch.

Ah, here is the subtle bug:

if (is_dir($path)) {
    get_directory_content($path);
    $is_file = true; // <--- THIS IS THE BUG
}
else{
    $is_file = true;
}

The variable $is_file is set to true regardless of whether the item was a directory or a file. The code then proceeds to call file_get_contents($path) if $is_file is true. Since directories are not files, file_get_contents will fail on any directory entry that passes this check.

The logic intends to recurse into directories and not read them. By setting $is_file = true inside the if (is_dir(...)) block, the code effectively tells itself "this item is a file" after it has just confirmed it is a directory. Consequently, when the script tries to read the content of that directory, PHP throws the EISDIR error because you cannot read bytes from a directory structure like you would from a text file.

The fix is straightforward: ensure $is_file is only set to true if is_dir() returns false.

if (is_dir($path)) {
    get_directory_content($path);
    // Do not mark directories as files for the content reading step
} else {
    $is_file = true;
}

Additionally, there is a security and performance consideration regarding how file_get_contents handles large files. The script sets memory_limit to -1, which removes the memory cap but does not address execution time or file size limits imposed by max_execution_time. Reading massive files into memory can still cause a script timeout even with unlimited memory, especially if the search string is found late in a large file. For production environments handling large datasets, streaming the file content using fread() or processing line-by-line is a more robust strategy that avoids loading entire files into RAM.

If you are building this functionality within a larger Laravel application, you might consider leveraging the filesystem package or creating a service class to handle these recursive operations safely, ensuring proper exception handling for EISDIR and other potential file system errors. While tools like LaravelCompany offer resources for modern PHP development, understanding the underlying PHP behaviors remains essential for debugging low-level issues like this one.

Finally, always validate that a path is actually a file before attempting to read it with file_get_contents. Adding an explicit check ensures that directories, symlinks to directories, or special files do not inadvertently trigger read attempts.

Our network