在 PHP 中,fseek() 函数用于设置文件指针的位置
以下是一个使用 fseek() 与其他函数(如 fread() 和 feof())配合读取文件的示例:
<?php$file = fopen("example.txt", "r");if (!$file) { die("Error opening file");}// 将文件指针移动到文件开头fseek($file, 0);// 读取文件内容while (!feof($file)) { $content = fread($file, 1024); // 每次读取 1024 字节 echo $content;}// 关闭文件fclose($file);?>在这个示例中,我们首先使用 fopen() 打开一个文件。然后,我们使用 fseek() 将文件指针移动到文件开头。接下来,我们使用 while 循环和 fread() 函数逐块读取文件内容,直到遇到文件结束符(feof() 返回 true)。最后,我们使用 fclose() 关闭文件。


