當透過 fopen('file','a+') 開啟檔案以進行讀寫時,檔案指標應該位於檔案的末尾。然而,即使檔案不是空的,ftell() 也會返回 int(0)。此外,似乎有兩個指標,一個用於讀取,另一個用於寫入,因為它在第一個操作(讀取或寫入)時的行為不同。
範例
<?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);
?>
結果
0 "123456789" 9
0 "123456789" 9
2 "3456789" 9
2 "" 2
0 "rr3456789aa" 11