PHP Tutorial - PHP ftruncate() Function






Definition

The ftruncate() function truncates an open file to the specified length.

Syntax

PHP ftruncate() Function has the following syntax.

ftruncate(file,size)

Parameter

ParameterIs RequiredDescription
fileRequired.Open file to truncate
sizeRequired.New file size

Return

PHP ftruncate() Function returns TRUE on success, or FALSE on failure.





Example

Truncates an open file to the specified length


<?php// w w w  . j  a  v  a2  s  .c  o  m
//check filesize
echo filesize("test.txt");
echo "\n";

$file = fopen("test.txt", "a+");
ftruncate($file,100);
fclose($file);

//Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?>

The code above generates the following result.