在PHP中,fseek()函數用于在文件中設置讀取或寫入的位置
<?php
$file = fopen('example.txt', 'r');
if ($file === false) {
die('Error opening file');
}
// 將文件指針移動到文件末尾
fseek($file, 0, SEEK_END);
// 檢查當前文件指針位置是否等于文件大小
if (ftell($file) === feof($file)) {
echo "File pointer is at the end of the file.";
} else {
echo "File pointer is not at the end of the file.";
}
// 將文件指針移回文件開頭
fseek($file, 0, SEEK_SET);
fclose($file);
?>
在這個示例中,我們首先打開一個名為example.txt的文件。然后,我們使用fseek()函數將文件指針移動到文件的末尾(偏移量為0,起始位置為SEEK_END)。接下來,我們使用ftell()和feof()函數檢查文件指針是否位于文件末尾。最后,我們將文件指針移回文件的開頭,并關閉文件。