note 89027 deleted from function.ftell by danbrown
| From: | danbrown@php.net | Date: | Wed, 18 Feb 2009 19:28:01 +0000 |
| Subject: | note 89027 deleted from function.ftell by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150813@lists.php.net to get a copy of this message | ||
Note Submitter: johnston-dot-joshua at gmail dot com
----
RE: append-only streams
My testing shows that ftell always reports 0 when you first open a file regardless of the mode it
was opened in.
If open a file in append mode (a) and you want ftell to report the actual position in the file
instead of the position relative to where you entered you can use fseek
<?php
// create a test file with 100 bytes in it
$file = 'temp.log';
$fp = fopen($file, 'w');
fwrite($fp, str_repeat('+', 100));
fclose($fp);
// append mode to show fseek
$fp = fopen($file, 'a');
echo ftell($fp), PHP_EOL; // reports 0 even though you are at char 100
fseek($fp, 0, SEEK_END); // seek to end (does nothing on append streams)
echo ftell($fp), PHP_EOL; // reports 100
fwrite($fp, str_repeat('+', 100));
echo ftell($fp), PHP_EOL; // reports 200
?>