ftruncate()函數(shù)可以將打開的文件截斷為指定的長度,并且成功時可以返回true,失敗時可以返回false。
bool ftruncate ( resource $handle , int $size )
該函數(shù)可以獲取文件指針,處理文件并將其截斷長度為 size。
<?php //檢查文件大小 echo filesize("/PhpProject/sample.txt"); echo "\n"; $file = fopen("/PhpProject/sample.txt", "a+"); ftruncate($file, 100); fclose($file); //清除緩存并再次檢查文件大小 clearstatcache(); echo filesize("/PhpProject/sample.txt"); ?>
輸出結(jié)果
49 100