PHP ftruncate() Function

Definition and Usage

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

Syntax

ftruncate(file,size)
Parameter Description
file Required. Specify the opened file to be truncated.
size Required. Specify the new file size.

Description

Accepts a file pointer file As a parameter, and truncate 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 on 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