Post by Joe1234 » Mon Aug 18, 2025 10:03 am

I got the following 500 error: [17-Aug-2025 21:17:36 America/New_York] /home/....../system/library/cache/file.php - line: 32 - Message: fread(): Argument #2 ($length) must be greater than 0 -

So I changed this:

Code: Select all

	public function get($key) {
		$files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');

		if ($files) {
			$handle = fopen($files[0], 'r');

			flock($handle, LOCK_SH);

			$data = fread($handle, filesize($files[0]));

			flock($handle, LOCK_UN);

			fclose($handle);

			return json_decode($data, true);
		}

		return false;
	}
To this:

Code: Select all

public function get($key) {
    $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');

    if (!empty($files) && file_exists($files[0]) && filesize($files[0]) > 0) {
        $handle = fopen($files[0], 'r');

        if ($handle) {
            // Lock for shared reading
            if (flock($handle, LOCK_SH)) {
                $data = fread($handle, filesize($files[0]));
                flock($handle, LOCK_UN); // Unlock
            } else {
                $data = '';
            }

            fclose($handle);

            return json_decode($data, true);
        }
    }
    return false;
}
I just wanted to check with anyone if it might affect anything with how the site operates with the cache and mess something up as cache manipulation is not my forte.

Thanks.

EDIT: Note, I implemented this solution after I deleted files that were filling up my server and refreshed the cache on in the admin and the problem did not resolve itself. So I'm not 100% certain this actually fixed the issue, or the deletion of the files finally caught up....but the site kicked in as soon as I saved this code.

A secondary question, when something like this happens (if this was due to no space being available), could I implement code that could send me an email that the server space is filled up, or does this cripple any action on the front end?

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by ADD Creative » Mon Aug 18, 2025 3:39 pm

You may be better checking the file size after the flock(). Until the file is locked it could still change.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by paulfeakins » Mon Aug 18, 2025 7:41 pm

Joe1234 wrote:
Mon Aug 18, 2025 10:03 am
I got the following 500 error: [17-Aug-2025 21:17:36 America/New_York] /home/....../system/library/cache/file.php - line: 32 - Message: fread(): Argument #2 ($length) must be greater than 0 -
In future, you need to tell us what you're doing and WHY?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Wed Aug 20, 2025 9:38 pm

ADD Creative wrote:
Mon Aug 18, 2025 3:39 pm
You may be better checking the file size after the flock(). Until the file is locked it could still change.
OK, I'll try that.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 22 guests