PHP ftruncate() Function

Definition and Usage

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

Syntax

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

Description

Accepts 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

Comment:The file will only be changed in append mode. In write mode, you must add fseek() Operation.

Comment: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