ftruncate

(PHP 4, PHP 5)

ftruncate -- Урезает файл до указанной длинны

Описание

bool ftruncate ( resource handle, int size )

Принимает файловый указатель handle и урезает соответствующий файл до размера size. Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Замечание: В версиях PHP ниже 4.3.3, ftruncate() возвращает значение integer 1 в случае успеха, вместо boolean TRUE.

См. также описание функций fopen() и fseek().



ftruncate
latet at poczta dot onet dot pl
13-Aug-2004 11: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
stevedegrace von yahoo point ca
16-Nov-2003 02: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 03: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 06: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: Mon, 14 Nov 2005