PHP 文件大小报告旧大小
以下代码是我编写的 PHP Web 服务的一部分。它需要一些上传的 Base64 数据,对其进行解码,然后将其附加到文件中。这一切都很好。
问题是,当我在追加操作之后读取文件大小时,我得到了追加操作之前文件的大小。
$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);
$newSize = filesize($filepath.$filename); // gives old file size
我做错了什么?
系统是:
- PHP 5.2.14
- Apache 2.2.16
- Linux 内核 2.6.18
The following code is part of a PHP web-service I've written. It takes some uploaded Base64 data, decodes it, and appends it to a file. This all works fine.
The problem is that when I read the file size after the append operation I get the size the file was before the append operation.
$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);
$newSize = filesize($filepath.$filename); // gives old file size
What am I doing wrong?
System is:
- PHP 5.2.14
- Apache 2.2.16
- Linux kernel 2.6.18
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在基于 Linux 的系统上,
filesize()
获取的数据是“statcached”。尝试在文件大小调用之前调用
clearstatcache();
。On Linux based systems, data fetched by
filesize()
is "statcached".Try calling
clearstatcache();
before the filesize call.根据 PHP 手册:
https://www.php.net/manual/en/function.filesize。 php
基本上,你必须在文件操作后清除统计缓存:
According to the PHP manual:
https://www.php.net/manual/en/function.filesize.php
Basically, you have to clear the stat cache after the file operation:
PHP 将其读取的所有文件元数据存储在缓存中,因此文件大小很可能已存储在该缓存中,您需要清除它。请参阅 clearstatcache 并在调用 < 之前调用它代码>文件大小。
PHP stores all file metadata it reads in a cache, so it's likely that the file size is already stored in that cache, and you need to clear it. See clearstatcache and call it before you call
filesize
.