|
|
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().
mike at mikeleigh dot com
04-Jan-2007 12:30
I have produced a number of tests below which walk through my findings of the ftruncate function. For the impatient among you ftruncate can be used to increase the size of the file and will fill the rest of the file with CHR 0 or ASCII NULL. It can be used as a very convenient way of making a 1Mb file for instance.
Test 1
<?php
$fp = fopen('test_1.txt', 'w+');
fwrite($fp, 'some text');
?>
The first test is only here to make sure that a file can be written with some text.
Test 2
<?php
$fp = fopen('test_2.txt', 'w+');
fwrite($fp, 'some text');
ftruncate($fp, 4);
?>
As expected the file has been truncated to 4 bytes.
Test 3
<?php
$fp = fopen('test_3.txt', 'w+');
fwrite($fp, 'some text');
ftruncate($fp, 40);
?>
Interestingly the file has increased from 9 bytes to 40 bytes. The remaining 31 bytes of the file are ASCII code 0 or NULL though.
Further notes can be found here http://mikeleigh.com/links/ftruncate
|