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.

Member Avatar for diafol

Just to check, you're only getting this output:

file empty so gonna write data to file.

Do a

echo "The file size is actually ".filesize($filename)." bytes.\n"; 

before line 12

Hi diafol,

Yes that's correct i only ever get

file empty so gonna write data to file.

i added the code you gave before line 12 and it outputs

The file size is actually 0 bytes.

yet in the file i have

1487071245 ; 582

Cheers.

Hi, use r+ to read and write from the top or a+ in case you want to append data:

$fopen = fopen($filename, 'r+');

With the w+ flag the file is cleared:

Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

See: http://php.net/fopen

Many thanks @cereal problem solved. Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.