ftruncate()

ftruncate() — Truncates a file to a given length.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Takes the filepointer, stream, and truncates the file to length, size.

Signature

ftruncate(resource $stream, int $size): bool

Parameters

ParameterTypeWhat it takes
$streamresourceThe file pointer.
$sizeintThe size to truncate to.

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

$filename = 'lorem_ipsum.txt'; $handle = fopen($filename, 'r+'); ftruncate($handle, rand(1, filesize($filename))); rewind($handle); echo fread($handle, filesize($filename)); fclose($handle);

See also

fopen(), fseek()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page ftruncate() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.