Hướng dẫn dùng ftell c trong PHP

- Hàm ftell() dùng để trả về vị trí hiện tại của con trỏ tập tin trong một tập tin đang được mở.

- Cú pháp:

ftell(tập tin đang được mở)

- Tôi có một cây thư mục như sau:

  • myCode.php
  • file
    • test.txt

- Phía dưới là nội dung của tập tin test.txt

Hướng dẫn dùng ftell c trong PHP

- Đoạn mã phía dưới là nội dung của tập tin myCode.php

";
    echo ftell($file);
    fclose($file);
?>

- Khi đó, trên màn hình sẽ hiển thị:

0
10

Hàm ftell() sẽ trả về vị trí hiện tại của con trỏ tệp tin.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápftell( $handle);

Trong đó:

  • $handle là file đã được mở trước đó bởi hàm fopen().

Kết quả trả về

Hàm sẽ trả về vị trí hiện tại của con trỏ tập tin, nếu xảy ra lỗi hàm sẽ trả về False.

Bài viết này được đăng tại [free tuts .net]

Ví dụ

Ví dụ đơn giản về hàm ftell():

Code

$fp = fopen('test.txt', 'r+');
echo ftell($fp) ." 
"; fseek($fp, 5); echo ftell($fp);

Tham khảo: php.net

- Hàm ftell() dùng để trả về vị trí hiện tại của con trỏ tập tin trong một tập tin đang được mở.

- Cú pháp:

ftell(tập tin đang được mở)

- Tôi có một cây thư mục như sau:

  • myCode.php
  • file
    • test.txt

- Phía dưới là nội dung của tập tin test.txt

- Đoạn mã phía dưới là nội dung của tập tin myCode.php

";
    echo ftell($file);
    fclose($file);
?>

- Khi đó, trên màn hình sẽ hiển thị:

0
10

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

ftellReturns the current position of the file read/write pointer

Description

ftell(resource $stream): int|false

Parameters

stream

The file pointer must be valid, and must point to a file successfully opened by fopen() or popen(). ftell() gives undefined results for append-only streams (opened with "a" flag).

Return Values

Returns the position of the file pointer referenced by stream as an integer; i.e., its offset into the file stream.

If an error occurs, returns false.

Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

Examples

Example #1 ftell() example

// opens a file and read some data
$fp fopen("/etc/passwd""r");
$data fgets($fp12);// where are we ?
echo ftell($fp); // 11fclose($fp);?>

See Also

  • fopen() - Opens file or URL
  • popen() - Opens process file pointer
  • fseek() - Seeks on a file pointer
  • rewind() - Rewind the position of a file pointer

Mindaugas

7 years ago

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:
$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

burninleo at gmx dot net

13 years ago

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 at webwriters dot de

16 years ago

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

17 years ago

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.

missilesilo at gmail dot com

15 years ago

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
$tell = ftell(STDIN);
var_dump($tell);
?>

[email protected]:/home/david# echo Hello World | ./test.php
int(0)
[email protected]:/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
$tell = ftell(STDIN);

if (

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

And the result is:

[email protected]:/home/david# echo Hello World | ./test.php
Something was piped: Hello World
[email protected]:/home/david# ./test.php
Nothing was piped

php at michielvleugel dot com

17 years ago

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";}

?>