Hi,
I wrote some code below, at the moment i'm testing so there's no database queries in the code but before i do that i'm hoping someone can help. The code below where it says if(filesize($filename) != 0)
always goes to else
even though the file is not 0 bytes and has 16 bytes of data in there.
I think it's easier to show my code (could be other errors in there but i'm checking each error as i go along, dealing with them 1 by 1). I get no php errors or anything so can only assume it's my if statement.
$filename = 'memberlist.txt';
$file_directory = dirname($filename);
$fopen = fopen($filename, 'w+');
// check is file exists and is writable
if(file_exists($filename) && is_writable($file_directory)){
// clear statcache else filesize could be incorrect
clearstatcache();
// check if file contains any data
if(filesize($filename) != 0){ // also tried !==
// read file into an array
$fread = file($filename);
// get current time
$current_time = time();
foreach($fread as $read){
$var = explode(';', $read);
$oldtime = $var[0];
$member_count = $var[1];
}
if($current_time - $oldtime >= 86400){
// 24 hours or more so we query db and write new member count to file
echo 'more than 24 hours has passed'; // for testing
} else {
// less than 24 hours so don't query db just read member count from file
echo 'less than 24 hours has passed'; // for testing
}
} else {
// else file is empty so we add data
$current_time = time().' ; ';
$member_count = 582; // this value will come from a database
fwrite($fopen, $current_time.$member_count);
fclose($fopen);
echo 'file empty so gonna write data to file.'; // for testing
}
} else {
// file either does not exist or cant be written to
echo 'file does not exist or is not writeable'; // for testing
}
Basically the code will be on a memberlist page which currently retrieves all members and counts how many members are registered.
The point in the script is if the time is less than 24 hours we read the member_count from file else if 24 hours or more has elapsed then we query database, get the member count and write new figure to file, it's to reduce queries on the memberlist page.
Thank you for any help. Cheers.