Debug School

rakesh kumar
rakesh kumar

Posted on

Uncaught TypeError: fread(): Argument #1 ($stream) must be of type resource, bool given

fopen(165278920654MPKyhIiy.jpg): Failed to open stream: No such file or directory.
Uncaught TypeError: fread(): Argument #1 ($stream) must be of type resource, bool given

Problem:

Image description

Solution

This error message indicates that you are trying to use the fread() function with an argument that is not of the expected type. Specifically, the function expects the first argument ($stream) to be a resource, but it is receiving a boolean value instead.

A possible reason for this error could be that the file you are trying to read was not opened correctly, or the file pointer was not passed correctly to the fread() function. To avoid this error, you need to ensure that the file is opened properly before calling fread(), and that the file pointer is passed correctly.

Here's an example of how to open a file and read its contents using fread():

$filename = "example.txt";
$file = fopen($filename, "r");
if ($file) {
    $content = fread($file, filesize($filename));
    fclose($file);
    echo $content;
} else {
    echo "Error opening file!";
}
Enter fullscreen mode Exit fullscreen mode

In this example, we first open the file using fopen() with the "r" mode to open it for reading. Then we check if the file was opened successfully, and if it was, we use fread() to read the entire contents of the file into a variable named $content. Finally, we close the file using fclose().

Image description

Top comments (0)