PHP ftruncate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, ftruncate() cuts an open file down to a given size, or makes it larger by adding empty bytes. It is the quick way to empty a file or trim off the end.

The file must be opened with a mode that allows writing, such as r+ or w. The function changes only the file size and does not move the file pointer.

Syntax

Call ftruncate() with the values you need. Start with this line.

PHP
$ok = ftruncate($fp, 5);
CallWhat it does
ftruncate($fp, 0)Empties the file completely.
ftruncate($fp, 5)Keeps only the first 5 bytes of the file.
ftruncate($fp, 100)Makes the file 100 bytes long, filling any new space with null bytes.

After ftruncate($fp, 0) the pointer still sits at its old position. Call rewind($fp) before writing again, or PHP will pad the start of the file with null bytes.

Library declarations
PHP
ftruncate(resource $stream, int $size): bool

Parameters

ParameterDescription
streamRequired. A file handle opened with a write mode such as r+, w or a+. A handle opened with r cannot be truncated.
sizeRequired. The new size of the file in bytes. It must be 0 or greater.

Return Value

Return ValueDescription
boolReturns true on success or false on failure, for example when the file was opened read only.

PHP Examples

Example 1 — Cut a File Down to 5 Bytes

Open a file for reading and writing, then keep only its first 5 bytes.

PHP
<?php
$path = sys_get_temp_dir() . '/ftruncate_demo.txt';
file_put_contents($path, 'Hello World');

$fp = fopen($path, 'r+');
ftruncate($fp, 5);
fclose($fp);

echo file_get_contents($path), PHP_EOL;
unlink($path);

How It Works

  1. The file starts with Hello World, which is 11 bytes long.
  2. fopen() uses mode r+ so the file can be both read and changed without being emptied.
  3. ftruncate($fp, 5) cuts everything after the first 5 bytes.
  4. Reading the file back shows only Hello.

Example 2 — Truncate a Read-Only Handle

A file opened with mode r cannot be changed. ftruncate() returns false and the content stays the same.

PHP
<?php
$path = sys_get_temp_dir() . '/ftruncate_edge.txt';
file_put_contents($path, 'Hello World');

$fp = fopen($path, 'r');

if (@ftruncate($fp, 5)) {
    echo "Truncated", PHP_EOL;
} else {
    echo "Truncate failed", PHP_EOL;
}

fclose($fp);
echo file_get_contents($path), PHP_EOL;
unlink($path);

How It Works

  1. Mode r opens the file for reading only.
  2. ftruncate() needs write access, so it fails and returns false.
  3. The @ sign hides the PHP warning, and the else branch prints a clear message.
  4. The file content is unchanged, as the last line shows.

Example 3 — Clear a File and Write New Content

Use ftruncate() with 0 to empty a file, rewind the pointer, and then write fresh data.

PHP
<?php
$path = sys_get_temp_dir() . '/ftruncate_clear.txt';
file_put_contents($path, 'old old old');

$fp = fopen($path, 'r+');
ftruncate($fp, 0);
rewind($fp);
fwrite($fp, 'new');
fclose($fp);

echo file_get_contents($path), PHP_EOL;
unlink($path);

How It Works

  1. ftruncate($fp, 0) removes all of the old content.
  2. rewind() moves the pointer back to byte 0, which is important because ftruncate() does not move it.
  3. fwrite() writes the word new at the start of the now empty file.
  4. The file ends up containing only new.

Frequently Asked Questions

It changes the size of an open file. A smaller size cuts off the end of the file, and a larger size extends it with null bytes.
Open the file with r+ and call ftruncate($fp, 0). Then call rewind($fp) before writing so the new data starts at the beginning.
The most common reason is that the file was opened read only, for example with mode r. Use r+, w, a or a similar write mode.
No. The pointer stays where it was. If it is past the new end of the file, a later write can leave a gap of null bytes.
Mode w empties the file the instant it opens. ftruncate() lets you open the file first, read it, and then decide how much to keep.

Next: fwrite()

fwrite() writes a string to an open file. It is the main function for saving text or data .

fwrite() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful