★ wanayoo — archive 1999 http://fr.php.net/manual/zh/function.fseek.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<fscanffstat>
view the version of this page
Last updated: Sun, 20 Feb 2005

fseek

(PHP 3, PHP 4 , PHP 5)

fseek -- 在文件指针中定位

说明

int fseek ( resource handle, int offset [, int whence])

在与 handle 关联的文件中设定文件指针位置。新位置,从文件头开始以字节数度量,是以 whence 指定的位置加上 offsetwhence 的值定义为:

SEEK_SET - 设定位置等于 offset 字节。
SEEK_CUR - 设定位置为当前位置加上 offset
SEEK_END - 设定位置为文件尾加上 offset。(要移动到文件尾之前的位置,需要给 offset 传递一个负值。)

如果没有指定 whence,默认为 SEEK_SET

成功则返回 0;否则返回 -1。注意移动到 EOF 之后的位置不算错误。

例子 1. fseek() 例子

<?php

$fp
= fopen('somefile.txt');

// read some data
$data = fgets($fp, 4096);

// move back to the begining of the file
// same as rewind($fp);
fseek($fp, 0);

?>

可能不能用于在 fopen() 中以 "http://" 或 "ftp://" 格式打开所返回的文件指针。

注: whence 参数是 PHP 4.0.0 之后增加的。

参见 ftell()rewind()



add a note add a note User Contributed Notes
fseek
Lutz ( l_broedel at gmx dot net )
14-Feb-2005 11:25
Based on the function below, provided by info at o08 dot com (thanks), the following should enable you to read a single line from a file, identified by the line number (starting with 1):

<?
  
function readLine ($linenum,$fh) {
      
$line = fgets ($fh, 4096);
      
$pos = -1;
      
$i = 0;

       while (!
feof($fh) && $i<($linenum-1)) {
          
$char = fgetc($fh);
           if (
$char != "\n" && $char != "\r") {
              
fseek($fh, $pos, SEEK_SET);
              
$pos ++;
           }
           else
$i ++;
       }
      
$line = fgets($fh);
       return
$line;
   }
//readLine()
?>
info at o08 dot com
15-Feb-2004 08:27
I think the function should be as following to deal any combination of cr & lf, no matter the line ends by cr, lf, cr-lf or lf-cr:

<?php
function getline ($handle) {
       while (!
feof($handle)) {
          
$char = fgetc($handle);
           if ((
$char == "\n") or ($char == "\r")) {
              
$char2 = fgetc($handle);
               if ((
$char2 != "\n") && ($char2 != "\r")) {
                  
fseek ($handle,-1,SEEK_CUR);
               }
               break;
           }
           else {
              
$buffer .= $char;
           }
       }
       return
$buffer;
}
?>
jim at lfchosting dot com
05-Nov-2003 03:03
Here is a function that returns the last line of a file.  This should be quicker than reading the whole file till you get to the last line.  If you want to speed it up a bit, you can set the $pos = some number that is just greater than the line length.  The files I was dealing with were various lengths, so this worked for me.

<?php
function readlastline($file)
{
      
$fp = @fopen($file, "r");
      
$pos = -1;
      
$t = " ";
       while (
$t != "\n") {
            
fseek($fp, $pos, SEEK_END);
            
$t = fgetc($fp);
            
$pos = $pos - 1;
       }
      
$t = fgets($fp);
      
fclose($fp);
       return
$t;
}
?>
aspyrine at hotmail dot com
11-Mar-2003 04:51
If you want to go to the end of a socket stream with fseek() you'll get the following error :
"stream does not support seeking"

feof() wont work eiver in a stream (ie. smtp)

You can move the pointer to the end with this command :
while(fgetc($fp)) {}
...so easy :-)
16-Sep-2002 10:25
Don't use filesize() on files that may be accessed and updated by parallel processes or threads (as the filesize() return value is maintained in a cache).
Instead lock the opened file and use fseek($fp,0,SEEK_END) and ftell($fp) to get the actual filesize if you need to perform a fread() call to read the whole file...
25-Aug-2002 02:12
The following call moves to the end of file (i.e. just after the last byte of the file):

牋牋fseek($fp, 0, SEEK_END);

It can be used to tell the size of an opened file when the file name is unknown and can't be used with the filesize() function:

牋牋fseek($fp, 0, SEEK_END);
牋牋$filesize = ftell($fp);

The following call moves to the begining of file:

牋牋fseek($fp, 0, SEEK_SET);

It is equivalent to:

牋牋rewind($fp);
dan at daniellampert dot com
01-Jan-2002 11:54
For all first-time users of the fseek() function, remember these three things:

1. to use a programming expression, fseek() is "base 0", so to prepare the file for writing at character 1, you'd say fseek($fp,0); and to prepare the file for writing at character $num, you'd say fseek($fp,($num-1));

2. here's the formula for accessing fixed-length records in a file (you need to seek the position of the end of the previous record):

  /* assumes the desired record number is in $rec_num */
  /* assumes the record length is in $rec_len */
  $pos = ( ($rec_num-1) * $rec_len );
  fseek($fp,$pos);

3. if you're using fseek() to write data to a file, remember to open the file in "r+" mode, example:

  $fp=fopen($filename,"r+");

Don't open the file in mode "a" (for append), because it puts the file pointer at the end of the file and doesn't let you fseek earlier positions in the file (it didn't for me!). Also, don't open the file in mode "w" -- although this puts you at the beginning of the file -- because it wipes out all data in the file.

Hope this helps.

<fscanffstat>
 Last updated: Sun, 20 Feb 2005
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Mon Feb 21 05:12:00 2005 CET