PHP Warning: fread(): Length parameter must be greater than 0 in setoptions.php on line 311 Error and Solution

One of my php script called setoptions.php is hosted on my new vps server. However, it is not working so I setup php error log as described here and, and I see the following warning repeated in my log file:

PHP Warning: fread(): Length parameter must be greater than 0 in /var/www/html/scripts/setoptions.php on line 311

The line no # 311 is as follows in my php script:
$fname = 'myappconfig.php';
$content = fread($fhandle,filesize($fname));

How do I fix this problem under Unix/Apache/php5 server?

To fix this problem, try the following solutions:

[1] Make sure file $fname exists i.e. myappconfig.php is in path.

[2] Make sure file myappconfig.php has correct permission. Apache or any other webserver needs read-only permission for reading file.

[3] Make sure file myappconfig.php exists and size is not zero i.e. make sure file is not empty.

[4] Use is_readable() to find out whether a file exists and is readable:

$fname = 'myappconfig.php';
if (is_readable($fname)) {
    $content = fread($fhandle,filesize($fname));
} else {
    echo 'The file is not readable.';
}

[5] Also use file_exists() to check whether a file or directory exists:

$fname = 'myappconfig.php';
 
if (file_exists($fname)) {
    // do_something like as follows?
    // $content = fread($fhandle,filesize($fname));
} else {
    echo "The file $fname does not exist.";
}

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts