search for in the  
<ftellfwrite>
Last updated: Thu, 19 May 2005

ftruncate

(PHP 4, PHP 5)

ftruncate -- Truncates a file to a given length

Description

bool ftruncate ( resource handle, int size )

Takes the filepointer, handle, and truncates the file to length, size. Returns TRUE on success or FALSE on failure.

Note: The file pointer is changed only in append mode. In write mode, additional fseek() call is needed.

Note: Prior to PHP 4.3.3, ftruncate() returns an integer value of 1 on success, instead of boolean TRUE.

See also fopen() and fseek().



User Contributed Notes
ftruncate
latet at poczta dot onet dot pl
13-Aug-2004 01:01
I want to report:
on Apache/2.0.48 (Linux/SuSE), PHP Version 4.3.3.

It is very different in "a" and in "r" modes.

After fopen in "a" mode, I don't have to fseek before ftruncate ($fp, 0) and even between ftruncate and the following fputs (if I need to write to the begining of the file).

But if I open in "r" mode and read some bytes, and then I want to ftruncate ($fp, 0) and write again - I do need to put fseek($fp, 0); just after ftruncate ($fp, 0).

Latet
rc at opelgt dot org
12-May-2004 08:35
I was confused by the different fopen modes (r, r+, w, w+, a, a+).
I wrote

<?php
$alt
= '1234567890';
$neu = '13579';

$datei = 'test.txt';
$dh = fopen ($datei,"w");
fwrite($dh, $alt);
fclose($dh);

$dh = fopen ($datei,"a+"); // just alter the mode to test others
rewind($dh);
echo
"Inhalt<BR>\nalt: ".fread($dh, filesize($datei))."<BR>\n";
ftruncate($dh, '0');
fwrite($dh, $neu);
rewind($dh);
echo
'neu: '.fread($dh, filesize($datei));
fclose($dh);
?>

to find out what happens:

1. For me ftruncate to a size of 0 takes the pointer to position 0 successfully. Tested under UNIX with PHP 4.2.3 and under MacOSX 10.3 with PHP 4.3.4
2. Writing to a file opened with 'a' or 'a+' always begins writing at the end of the file. A rewind before doesnt help! ('a' for always append!)

macrudi
stevedegrace von yahoo point ca
16-Nov-2003 04:07
I am using PHP 4.3.2 and I can confim you do need to seek to the beginning of the file before truncating and writing. I found that failing to rewind before truncating and writing caused my output file to be uninterpretable by some applications, but by rewinding first the desired result was achieved.
jfb
05-Sep-2003 05:30
> ftruncate does not alter the position of the file pointer

Are you sure ? On linux with PHP 4.2.2  there is no problem if I don't seek to the beginning of the file before writing.
jim dot hatfield at insignia dot com
15-Jun-2001 08:58
ftruncate does not alter the position of the file pointer. If you edit a file in-place by opening for reading and writing, reading the file, truncating and writing out again, you have to seek to the beginning before writing or you get as many NULLs as there were bytes in the original file, then your new content.

<ftellfwrite>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT