ftell

(PHP 3, PHP 4, PHP 5)

ftell -- Сообщает текущее смещение чтения/записи файла

Описание

int ftell ( resource handle )

Возвращает смещение файлового указателя, ассоциируемого с handle.

При возникновении ошибки, возвращает FALSE.

Файловый указатель должен быть действующим указателем на поток, открытый функциями fopen() или popen(). ftell() возвращает неопределённые результаты для потоков "дописать в конец" (открытых с флагом "a" flag).

Пример 1. Пример использования функции ftell()

<?php

// открываем файл и читаем немного данных
$fp = fopen("/etc/passwd", "r");
$data = fgets($fp, 12);

// где же мы ?
echo ftell($fp); // 11

fclose($fp);

?>

См. также описание функций fopen(), popen(), fseek() и rewind().



ftell
mbirth at webwriters dot de
21-Oct-2005 11:09
Attention! If you open a file with the "text"-modifier (e.g. 'rt') and the file contains \r\n as line-endings, ftell() returns the position as if there were only \n as line-endings.

Example:
If the first line only contains 1 char followed by \r\n, the start of the second line should be position 3. (1char + \r + \n = 3 bytes) But ftell() will return 2 - ignoring one byte. If you call ftell() in line 3, the value will differ from the real value by 2 bytes. The error gets greater with every line.

(Watched this behavior in PHP 5.0.4 for Windows.)

BUT: fseek() works as expected - using the true byte values.
mweierophinney at gmail dot com
21-Jun-2005 10:00
Actually, ftell() gives more than an undefined result for append only streams; it gives the offset from the end of the file as defined before any data was appended. So if you open a file that had 3017 characters, and append 41 characters, and then execute ftell(), the value returned will be 41.
php at michielvleugel dot com
01-Jun-2005 03:19
When trying to determine whether or not something was piped into a command line script, it is not smart to do a fgets(STDIN), because it will wait indefenitely if nothing is piped. Instead, I found ftell on STDIN to be very handy: it will return an integer of zero when something was piped, and nothing if nothing was piped to the script.

#!/usr/bin/php4 -q
<?
#following will hang if nothing is piped:
#$sometext = fgets(STDIN, 256)

$tell = ftell(STDIN);

if (
is_integer($tell)==true)
  {echo
"Something was piped: ".fread(STDIN,256)."\n";}
else
  {echo
"Nothing was piped\n";}

?>

<fstatftruncate>
 Last updated: Tue, 15 Nov 2005