ftruncate

Truncates a file to a given length

Syntax

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

Parameters

handle

The file pointer.

Note: The handle must be open for writing.

size

The size to truncate to.

Note: If size is larger than the file then the file is extended with null bytes. If size is smaller than the file then the file is truncated to that size.

Return

Returns TRUE on success or FALSE on failure.

Examples

1

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';
$mode = 'r+';

$handle = fopen($filename, $mode);

    echo fread($handle, filesize($filename)) . PHP_EOL;

    ftruncate($handle, 2);

    rewind($handle);
    echo fread($handle, filesize($filename));

fclose($handle);

?>
Hello
He
HomeMenu