English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP ftruncate() Function Usage and Example

PHP Filesystem Reference Manual

The ftruncate() function can truncate the opened file to the specified length and can return true on success, and false on failure.

Syntax

bool ftruncate ( resource $handle , int $size )

This function can obtain a file pointer, process the file, and truncate its length to size.

Online Example

<?php
   //Check file size
   echo filesize("/PhpProject/sample.txt");
   echo "\n";
   $file = fopen("/PhpProject/sample.txt", "a+");
   
   ftruncate($file, 100);
   fclose($file);
   
   //Clear cache and check file size again
   clearstatcache();
   
   echo filesize("/PhpProject/sample.txt");
?>

Output Result

49
100

PHP Filesystem Reference Manual