• PHP Video Tutorials

PHP - Function ftruncate()



The ftruncate() function can truncate an open file to a specified length, and can return true on success or false on failure.

Syntax

bool ftruncate ( resource $handle , int $size )

This function can take the filepointer, handle, and truncates the file to length, size.

Example

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

Output

49
100
php_function_reference.htm
Advertisements