PHP 实时获取文件大小

要将一个大的数据写入文件,但是不想写入一个,想写成一个文件xM,就需要实时判断文件大小,如果不清理文件状态缓存的话,无法实现效果。


clearstatcache() 函数清除文件状态缓存。

clearstatcache() 函数会缓存某些函数的返回信息,以便提供更高的性能。但是有时候,比如在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存,以便获得正确的结果。要做到这一点,就需要使用 clearstatcache() 函数。

会进行缓存的函数,即受 clearstatcache() 函数影响的函数:

    • 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()
 1 //信息存入文件,每个文件2m,
 2                 $max_size       = 1024 * 1024 * 2;
 3 
 4                 $v              = str_repeat( 'hello', 20 );
 5 
 6                 $tmp_i          = 1;
 7                 $handler        = null;
 8                 //foreach( Dinfo::gAllDocListHandle()->each() as $v ) {
 9                 for( $i = 0; $i <= 100000; $i ++ ){
10                     if( is_null( $handler ) ){
11                         $handler        = fopen( $file_path, 'ab' );
12                         flock( $handler, LOCK_EX );
13                     }
14 
15                     $tmp_val            = json_encode( $v ) . "\r\n";
16                     fwrite( $handler, $tmp_val, mb_strlen( $tmp_val, 'utf8' )  );
17                     clearstatcache();
18 
19                     if( filesize( $file_path ) >= $max_size ) {
20                         flock( $handler, LOCK_UN );
21                         fclose( $handler );
22                         $handler    = null;
23                         $file_path  = $cache_dir . 'allDoc.' . $tmp_i++ . '.txt';
24                     }
25                 }

 

posted @ 2016-11-17 15:51  栋的博客  阅读(6592)  评论(0编辑  收藏  举报
深入理解php php扩展开发 docker mongodb