note 116885 added to function.ftell
| From: | Mindaugas | Date: | Sun, 15 Mar 2015 21:38:01 +0000 |
| Subject: | note 116885 added to function.ftell | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201950@lists.php.net to get a copy of this message | ||
When opening a file for reading and writing via fopen('file','a+') the file
pointer should be at the end of the file. However ftell() returns int(0) even if the file is not
empty. Also it seems that there is two pointers, first for reading and second for writing, because
it acts differently on first operation (reading or writing).
Example:
<?php
$file = fopen('counter.txt', 'w');
fwrite($file, '123456789');
fclose($file);
$file = fopen('counter.txt', 'r');
echo ftell($file) . ' "' . fgets($file) . '" ' . ftell($file) .
PHP_EOL;
fclose($file);
$file = fopen('counter.txt', 'a+');
echo ftell($file) . ' "' . fgets($file) . '" ' . ftell($file) .
PHP_EOL;
fclose($file);
$file = fopen('counter.txt', 'r+');
fwrite($file, 'rr');
echo ftell($file) . ' "' . fgets($file) . '" ' . ftell($file) .
PHP_EOL;
fclose($file);
$file = fopen('counter.txt', 'a+');
fwrite($file, 'aa');
echo ftell($file) . ' "' . fgets($file) . '" ' . ftell($file) .
PHP_EOL;
fclose($file);
$file = fopen('counter.txt', 'r');
echo ftell($file) . ' "' . fgets($file) . '" ' . ftell($file) .
PHP_EOL;
fclose($file);
?>
Result:
0 "123456789" 9
0 "123456789" 9
2 "3456789" 9
2 "" 2
0 "rr3456789aa" 11
----
Server IP: 72.52.91.14
Probable Submitter: 79.133.227.85
----
Manual Page -- http://php.net/manual/en/function.ftell.php
Edit -- https://master.php.net/note/edit/116885
Del: integrated -- https://master.php.net/note/delete/116885/integrated
Del: useless -- https://master.php.net/note/delete/116885/useless
Del: bad code -- https://master.php.net/note/delete/116885/bad+code
Del: spam -- https://master.php.net/note/delete/116885/spam
Del: non-english -- https://master.php.net/note/delete/116885/non-english
Del: in docs -- https://master.php.net/note/delete/116885/in+docs
Del: other reasons-- https://master.php.net/note/delete/116885
Reject -- https://master.php.net/note/reject/116885
Search -- https://master.php.net/manage/user-notes.php