Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: ftell - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ftruncate" width="11" height="7"/> <fstat
Last updated: Thu, 31 May 2007

view this page in

ftell

(PHP 4, PHP 5)

ftell — ファイルポインタから読み書きの位置を取得する

説明

int ftell ( resource $handle )

handleが示すファイルポインタの位置、 すなわちファイル・ストリーム上のオフセットを返します。

エラーが起こった場合 FALSE を返します。

ファイルポインタは有効なものでなければならず、また fopen()popen() で正常にオープンされたファイルを指している必要があります。 ftell() は、("a" フラグ付きでオープンされた) 追加のみ可能なストリームに対する結果も未定義です。

例 632. ftell() の例

<?php

// ファイルをオープンし、データを読み込む
$fp = fopen("/etc/passwd", "r");
$data = fgets($fp, 12);

// どこにいるんだ ?
echo ftell($fp); // 11

fclose($fp);

?>

fopen()popen()fseek() および rewind() も参照ください。



ftruncate" width="11" height="7"/> <fstat
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
ftell
missilesilo at gmail dot com
28-Feb-2007 08:02
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
mbirth at webwriters dot de
22-Oct-2005 03: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
22-Jun-2005 02: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 07: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";}

?>

ftruncate" width="11" height="7"/> <fstat
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites