note 72136 added to function.ftruncate
| From: | mike at mikeleigh dot com | Date: | Thu, 04 Jan 2007 08:30:16 +0000 |
| Subject: | note 72136 added to function.ftruncate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121271@lists.php.net to get a copy of this message | ||
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
/*
Test 1: Write "some text" to a file.
Result: The text "some text" should be present in test_1.txt
*/
$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
/*
Test 2: Write "some text" to a file and ftruncate the file to 4 bytes.
Result: The text "some" should be present in test_2.txt as the file will have been
truncated to 4 bytes.
*/
$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
/*
Test 3: Write "some text" to a file and ftruncate the file to 40 bytes.
Result: The text "some text" should be present in test_3.txt as the file will have been
truncated to 40 bytes.
*/
$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
----
Server IP: 83.138.144.80
Probable Submitter: 87.74.62.77
----
Manual Page -- http://www.php.net/manual/en/function.ftruncate.php
Edit -- https://master.php.net/note/edit/72136
Del: integrated -- https://master.php.net/note/delete/72136/integrated
Del: useless -- https://master.php.net/note/delete/72136/useless
Del: bad code -- https://master.php.net/note/delete/72136/bad+code
Del: spam -- https://master.php.net/note/delete/72136/spam
Del: non-english -- https://master.php.net/note/delete/72136/non-english
Del: in docs -- https://master.php.net/note/delete/72136/in+docs
Del: other reasons-- https://master.php.net/note/delete/72136
Reject -- https://master.php.net/note/reject/72136
Search -- https://master.php.net/manage/user-notes.php