溫馨提示×

PHP中fseek如何處理文件結束

PHP
小樊
99
2024-12-02 13:40:43
欄目: 編程語言

在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()函數檢查文件指針是否位于文件末尾。最后,我們將文件指針移回文件的開頭,并關閉文件。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女