PHPCriticalApril 19, 2026

Runtime Error

Fatal error: Uncaught Error: Cannot pass string to argument 1 of fgets(), expect parameter of type resource

What This Error Means

This error occurs when a PHP function that expects a resource (like a file pointer) is passed a string instead.

Why It Happens

The error typically happens when a developer accidentally passes a string to a function that is expecting a resource. This can occur when a file has not been properly opened with fopen() or file_get_contents() before being passed to a function that works with files.

How to Fix It

  1. 1To fix this error, make sure to pass the correct type of argument to the function. If the function expects a resource, open the file with fopen() or file_get_contents() before passing it to the function. Alternatively, use the correct function that accepts a string, such as file_get_contents().

Example Code Solution

❌ Before (problematic code)
PHP
$file_content = fgets('example.txt');
✅ After (fixed code)
PHP
$file = fopen('example.txt', 'r');
$file_content = fgets($file);
fclose($file);

Fix for Fatal error: Uncaught Error: Cannot pass string to argument 1 of fgets(), expect parameter of type resource

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error