PHP ftruncate() function

Definition and Usage

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

Syntax

ftruncate(file,size)
Parameters Description
file Required. Specify the open file to be truncated.
size Required. Specify the new file size.

Description

Accepts a file pointer file As a parameter, and cut the file size to size. If successful, it returns TRUE; otherwise, it returns FALSE.

Tips and Comments

Note:The file will only be changed in append mode. In write mode, it must be added fseek() Operation.

Note:Before PHP 4.3.3, ftruncate() returns an integer value 1 upon success, not a boolean value TRUE.

Example

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

Output similar to:

792
100