HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

ftruncate

Description

The ftruncate of Filesystem for PHP truncates a file to a given length.

Syntax

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

Parameters

stream

The file pointer.

NOTE: The stream 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.

NOTE: 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 · stream size

<?

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

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

    $size = 2;

    $return = ftruncate($stream, $size);

    var_export($return);

fclose($stream);
true

2

<?

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

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

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

    $size = 2;

    ftruncate($stream, $size);

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

fclose($stream);
hello
he