PHP 即時擷取檔案大小

來源:互聯網
上載者:User

標籤:這一   刪除   info   tca   txt   資訊   技術分享   檢查   class   

要將一個大的資料寫入檔案,但是不想寫入一個,想寫成一個檔案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                 }

 

PHP 即時擷取檔案大小

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.