PHP.mk документација

ftell

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.ftell.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.ftell.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
ftell

Референца за `function.ftell.php` со подобрена типографија и навигација.

function.ftell.php

ftell

(PHP 4, PHP 5, PHP 7, PHP 8)

ftellЈа враќа тековната позиција на покажувачот за читање/запишување на датотеката

= NULL

ftell(resource $stream): int|false

Ја враќа позицијата на покажувачот на датотеката наведен од stream.

Параметри

stream

Показалецот на датотеката мора да биде валиден и мора да покажува на датотека успешно отворена од fopen() or popen(). ftell() дава неодредени резултати за потоци само за додавање (отворени со знамето "a").

Вратени вредности

Ја враќа позицијата на покажувачот на датотеката наведен од stream како цел број; т.е., неговиот офсет во протокот на датотеката.

Ако се случи грешка, враќа false.

Забелешка: Бидејќи типот на цел број на PHP е потпишан и многу платформи користат 32-битни цели броеви, некои функции за датотечниот систем може да вратат неочекувани резултати за датотеки поголеми од 2GB.

Примери

Пример #1 ftell() example

<?php

// opens a file and read some data
$fp = fopen("/etc/passwd", "r");
$data = fgets($fp, 12);

// where are we ?
echo ftell($fp); // 11

fclose($fp);

?>

Види Исто така

  • fopen() - Отвора датотека или URL
  • popen() - Поставува период на чекање на поток
  • fseek() - Бара на покажувачот на датотека
  • rewind() - Враќање на позицијата на покажувачот на датотека

Белешки од корисници 8 белешки

Mindaugas
пред 11 години
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
d9k at ya dot ru
3 години пред
Had to use 

<?php
posix_isatty(STDIN) == false
?>

instead of 

<?php
ftell(STDIN) !== false
?>

after upgrade from php 7 to 8 on ubuntu.
burninleo на gmx dot net
пред 16 години
When opening a file to append via fopen('file','ab') the file pointer should be at the end of the file. However ftell() returns int(0) even if the file is not empty and even after writing some text into the file.
mbirth на webwriters dot de
20 години пред
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 на gmail точка com
20 години пред
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.
d9k at ya dot ru
3 години пред
Had to use 

<?php
posix_isatty(STDIN) == false
?>

instead of 

<?php
ftell(STDIN) !== false
?>

after upgrade from php 7 to 8 on ubuntu.
php at michielvleugel dot com
20 години пред
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";}

?>
missilesilo at gmail dot com
19 години пред
In response to php at michielvleugel dot com:

This does not seem to be the case with PHP 5.2.0 and FreeBSD 5.4.

#!/usr/local/bin/php
<?php
$tell = ftell(STDIN);
var_dump($tell);
?>

root@localhost:/home/david# echo Hello World | ./test.php
int(0)
root@localhost:/home/david# ./test.php
int(6629927)

When something is piped to the script, it returns an integer value of 0, however, it also returns an integer when nothing is piped to the script.

The code should  be modified to this:

#!/usr/local/bin/php
<?php
$tell = ftell(STDIN);

if ($tell === 0)
    echo "Something was piped: " . fread(STDIN,256) . "\n";
else
    echo "Nothing was piped\n";
?>

And the result is:

root@localhost:/home/david# echo Hello World | ./test.php
Something was piped: Hello World
root@localhost:/home/david# ./test.php
Nothing was piped
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.