It seems the function does not accept negative values for $size, and it doesn't do anything if you pass one (it doesn't print an error and it doesn't truncate anything).
It would've been nice if it would truncate from the beginning when passed a negative value for $size.
ftruncate
(PHP 4, PHP 5)
ftruncate — Truncates a file to a given length
Descrierea
bool ftruncate
( resource $handle
, int $size
)
Takes the filepointer, handle , and truncates the file to length, size .
Parametri
- handle
-
The file pointer.
Notă: The handle must be open for writing.
- size
-
The size to truncate to.
Notă: If size is larger than the file it is extended with null bytes.
If size is smaller than the extra data will be lost.
Valorile întroarse
Întoarce valoarea TRUE în cazul succesului sau FALSE în cazul eşecului.
Istoria schimbărilor
| Versiunea | Descriere |
|---|---|
| PHP 4.3.3 | Prior to this release ftruncate() returned an integer value of 1 on success, instead of boolean TRUE. |
Note
Notă: The file pointer is not changed.
ftruncate
znupi69NOSPAMHERE at gmail dot com
11-Mar-2008 05:09
11-Mar-2008 05:09
rc at opelgt dot org
06-Jan-2008 02:49
06-Jan-2008 02:49
Writing after ftruncate
I didnt expect that I can write in the middle of nowhere. I thought that I would write at the beginning of the file but the first 4 bytes were filled automatically with NULLs followed by "56":
<?php
$str1 = 1234;
$str2 = 56;
$datei = "test.txt";
$dh = fopen($datei,"w");
fwrite($dh, $str1);
fclose($dh);
$dh = fopen ($datei,"r+");
echo "content: ".fread($dh, filesize($datei))."<br>";
echo "pointer after fread at: ".ftell($dh)."<br>";
ftruncate($dh, 0);
echo "pointer after truncate at: ".ftell($dh)."<br>";
fwrite($dh, $str2);
echo "pointer after fwrite at: ".ftell($dh)."<br>";
rewind($dh);
echo "pointer after rewind at: ".ftell($dh)."<br>";
$str = fread($dh, 6);
echo "content: $str<br>in ASCII: ";
for($i = 0; $i < 6; $i++)
echo ord($str{$i})."-";
fclose($dh);
/*
OUTPUT:
content: 1234
pointer after fread at: 4
pointer after truncate at: 4
pointer after fwrite at: 6
pointer after rewind at: 0
content: 56
in ASCII: 0-0-0-0-53-54
*/
?>
So not only ftruncate is filling an empty file up with NULLs as in the note before. Fread is filling leading space with NULLs too.
mike at mikeleigh dot com
04-Jan-2007 04:30
04-Jan-2007 04: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
/*
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
