php filesize() 方法返回的文件大小异常
问题描述:需要生成一个 700k 大小左右的文件,相关代码如下:
<?php $handle = fopen('./split.log', 'a+'); $result = 0; while( $result <= 700*1024) { for($number = 0; $number <= 10; $number++) { fwrite($handle, "{$number}\t"); } fwrite($handle, "\n"); $result = filesize('./split.log'); echo $result,"\n"; sleep(2); } fclose($handle); ?>
但是发现文件的大小一直不变,输出如下:
问题原因:
PHP 将缓存下面这些函数的返回信息以提供更快的性能。然而在某些情况下,你可能想清除被缓存的信息。例如如果在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存。这种情况下,可以用 clearstatcache() 函数来清除被 PHP 缓存的该文件信息。
stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype() 和 fileperms()。
详细信息可以查询 php 官方文档对于 clearstatcache() 方法的说明,链接为:http://www.php.net/manual/zh/function.clearstatcache.php

浙公网安备 33010602011771号