即使文件大小不是0字节,filesize()始终读取0字节 [英] filesize() always reads 0 bytes even though file size isn't 0 bytes

查看:93
本文介绍了即使文件大小不是0字节,filesize()始终读取0字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试下面,在下面编写了一些代码,因此代码中没有数据库查询.

I wrote some code below, at the moment I'm testing so there's no database queries in the code.

下面的代码表示 if(filesize($ filename)!= 0)的代码始终转到 else ,即使该文件不是0字节且具有16个字节那里的数据.我无处可去,似乎总是以为文件是0字节.

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 am getting nowhere, it just always seems to think file is 0 bytes.

我认为显示代码更容易(其中可能有其他错误,但是我正在检查每个错误,并逐一处理).我没有任何PHP错误或任何错误.

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 one by one). I get no PHP errors or anything.

$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();

    // for testing, shows 0 bytes even though file is 16 bytes
    // file has inside without quotes:   '1487071595 ; 582'
    echo "The file size is actually ".filesize($filename)." bytes.\n";

    // check if file contains any data, also tried !==
    // always goes to else even though not 0 bytes in size
    if(filesize($filename) != 0){

        // 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 { // WE ALWAYS END UP HERE
        // 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 "The file is empty so write new data to file. File size is actually ".filesize($filename)." bytes.\n"; 
    }

} else {
    // file either does not exist or cant be written to
    echo 'file does not exist or is not writeable'; // for testing
}

基本上,该代码将在成员列表页面上,该页面当前会检索所有成员并计算已注册的成员数.脚本中的要点是,如果时间少于24小时,则从文件中读取member_count;否则,如果经过了24小时以上,则我们查询数据库,获取成员数并将新图形写入文件,这是为了减少对数据库的查询.成员列表页面.

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.

更新1:

此代码:

echo文件大小实际上是" .filesize($ filename).字节.\ n";

即使不是0字节,也始终输出以下内容.

always outputs the below even though it's not 0 bytes.

文件大小实际上是0个字节.

The file size is actually 0 bytes.

也尝试过

var_dump(filesize($ filename));

输出:

int(0)

推荐答案

您正在使用:

fopen($filename, "w+") 

根据手册 w + 的意思是:

开放供阅读和写作;将文件指针放在文件的开头,并将文件截断为零长度.如果该文件不存在,请尝试创建它.

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.

因此文件大小为0是正确的.

So the file size being 0 is correct.

您可能需要 r +

这篇关于即使文件大小不是0字节,filesize()始终读取0字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆