Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Cache problems with function.fread


Guest

Recommended Posts

Posted

Maybe I can't count down to lne 60 or more likely I don't know what I'm doing, but can anyone help me get rid of this error message. I believe it is this line that is causing the problems:

 

if ($fp = @fopen($filename, 'w')) {

 

Warning: fread() [function.fread]: Length parameter must be greater than 0. in /home/robertea/public_html/catalog/includes/functions/cache.php on line 60

 

<?php

/*

$Id: cache.php 1739 2007-12-20 00:52:16Z hpdl $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright © 2006 osCommerce

 

Released under the GNU General Public License

*/

 

////

//! Write out serialized data.

// write_cache uses serialize() to store $var in $filename.

// $var - The variable to be written out.

// $filename - The name of the file to write to.

function write_cache(&$var, $filename) {

$filename = DIR_FS_CACHE . $filename;

$success = false;

 

// try to open the file

if ($fp = @fopen($filename, 'w')) {

// obtain a file lock to stop corruptions occuring

flock($fp, 2); // LOCK_EX

// write serialized data

fputs($fp, serialize($var));

// release the file lock

flock($fp, 3); // LOCK_UN

fclose($fp);

$success = true;

}

 

return $success;

}

 

////

//! Read in seralized data.

// read_cache reads the serialized data in $filename and

// fills $var using unserialize().

// $var - The variable to be filled.

// $filename - The name of the file to read.

function read_cache(&$var, $filename, $auto_expire = false){

$filename = DIR_FS_CACHE . $filename;

$success = false;

 

if (($auto_expire == true) && file_exists($filename)) {

$now = time();

$filetime = filemtime($filename);

$difference = $now - $filetime;

 

if ($difference >= $auto_expire) {

return false;

}

}

 

// try to open file

if ($fp = @fopen($filename, 'r')) {

// read in serialized data

$szdata = fread($fp, filesize($filename));

fclose($fp);

// unserialze the data

$var = unserialize($szdata);

 

$success = true;

}

 

return $success;

}

 

////

//! Get data from the cache or the database.

// get_db_cache checks the cache for cached SQL data in $filename

// or retreives it from the database is the cache is not present.

// $SQL - The SQL query to exectue if needed.

// $filename - The name of the cache file.

// $var - The variable to be filled.

// $refresh - Optional. If true, do not read from the cache.

function get_db_cache($sql, &$var, $filename, $refresh = false){

$var = array();

 

// check for the refresh flag and try to the data

if (($refresh == true)|| !read_cache($var, $filename)) {

// Didn' get cache so go to the database.

// $conn = mysql_connect("localhost", "apachecon", "apachecon");

$res = tep_db_query($sql);

// if ($err = mysql_error()) trigger_error($err, E_USER_ERROR);

// loop through the results and add them to an array

while ($rec = tep_db_fetch_array($res)) {

$var[] = $rec;

}

// write the data to the file

write_cache($var, $filename);

}

}

 

////

//! Cache the categories box

// Cache the categories box

function tep_cache_categories_box($auto_expire = false, $refresh = false) {

global $cPath, $language, $languages_id, $tree, $cPath_array, $categories_string;

 

$cache_output = '';

 

if (($refresh == true) || !read_cache($cache_output, 'categories_box-' . $language . '.cache' . $cPath, $auto_expire)) {

ob_start();

include(DIR_WS_BOXES . 'categories.php');

$cache_output = ob_get_contents();

ob_end_clean();

write_cache($cache_output, 'categories_box-' . $language . '.cache' . $cPath);

}

 

return $cache_output;

}

 

////

//! Cache the manufacturers box

// Cache the manufacturers box

function tep_cache_manufacturers_box($auto_expire = false, $refresh = false) {

global $HTTP_GET_VARS, $language;

 

$cache_output = '';

 

$manufacturers_id = '';

if (isset($HTTP_GET_VARS['manufacturers_id']) && is_numeric($HTTP_GET_VARS['manufacturers_id'])) {

$manufacturers_id = $HTTP_GET_VARS['manufacturers_id'];

}

 

if (($refresh == true) || !read_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id, $auto_expire)) {

ob_start();

include(DIR_WS_BOXES . 'manufacturers.php');

$cache_output = ob_get_contents();

ob_end_clean();

write_cache($cache_output, 'manufacturers_box-' . $language . '.cache' . $manufacturers_id);

}

 

return $cache_output;

}

 

////

//! Cache the also purchased module

// Cache the also purchased module

function tep_cache_also_purchased($auto_expire = false, $refresh = false) {

global $HTTP_GET_VARS, $language, $languages_id;

 

$cache_output = '';

 

if (isset($HTTP_GET_VARS['products_id']) && is_numeric($HTTP_GET_VARS['products_id'])) {

if (($refresh == true) || !read_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id'], $auto_expire)) {

ob_start();

include(DIR_WS_MODULES . FILENAME_ALSO_PURCHASED_PRODUCTS);

$cache_output = ob_get_contents();

ob_end_clean();

write_cache($cache_output, 'also_purchased-' . $language . '.cache' . $HTTP_GET_VARS['products_id']);

}

}

 

return $cache_output;

}

?>

 

thanks in advance.

 

Mike

Posted

Looks to me like the function read_cache() is the one having the problem when it hits this function call:

 

$szdata = fread($fp, filesize($filename));

 

$filename appears to not be properly set. Either the DIR_FS_CACHE directory doesn't exist or is not readable by the webserver or DIR_FS_CACHE isn't defined properly. Or maybe the $filename parameter isn't being passed properly to the function. Check those things and see which one it is.

Posted
You need to define CACHE in the includes/configure.php file.

 

Vger

 

You were right, I went and checked and it wasn't there, so I added:

 

define('DIR_FS_CACHE', '/home/robertea/public_html/catalog/cache/');

 

and I still get the same error message. What am I doing wrong?

 

Mike

  • 2 weeks later...
Posted

I have the same problem. My cache directory is correctly set up and permissions are 777.

But no cache files are created and the function read_cache() error message still appears. Any ideas?

  • 1 month later...
Posted
I have the same problem. My cache directory is correctly set up and permissions are 777.

But no cache files are created and the function read_cache() error message still appears. Any ideas?

 

I am getting the same error. I turned cache off and the error went away. Can somebody explain why this "cache" is important, useful and/or necessary? Or how to fix this particular problem?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...